【发布时间】:2015-10-23 15:13:50
【问题描述】:
我有一个包含两个表的视图模型,但是如果我正在更新我的实体框架,例如
查看模型:
public class MyViewModel
{
public IEnumerable<Telephone_Search.Models.tbl_pics> images;
public IEnumerable<Telephone_Search.Models.tbl_users> users;
}
控制器:
[HttpPost]
public ActionResult Index(tbl_pics pic, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
file.SaveAs(HttpContext.Server.MapPath("~/Images/")
+ file.FileName);
byte[] data = new byte[] { };
using (var binaryReader = new BinaryReader(file.InputStream))
{
data = binaryReader.ReadBytes(file.ContentLength);
}
pic.picture = file.FileName;
pic.user_no = 20173;
db.tbl_pics.Add(pic);
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View(pic);
}
然后在我的剃刀视图中使用视图模型,例如@model ViewModel 并尝试通过以下方式遍历用户:
@foreach (var img in Model.images) {
<img src="~/images/@pic.picture" width="100" height="100" />
}
值返回为空?
谢谢
【问题讨论】:
-
请发布您的控制器代码以查看视图的服务方式。
-
控制器的更多代码会很有用
-
看起来您的控制器代码正在返回
img并且您的视图期望具有Users属性的视图模型。你确定你传递的是正确的值吗? -
@ToddSprang 我不确定如何保存对实体框架的更改并将值传递给视图模型?
-
@Djeroen ,代码已更新!
标签: c# entity-framework model-view-controller viewmodel