返回视图模型无法识别 entity framework

Returning view model cannot recognize entity framework

型号:

Public partial class tbl_pictures {

public int picture_id
public string picture_content
public int user_no (foreign key)

public virtual tbl_user tbl_user {get;set;}
}

视图模型:

public class MyViewModel 
public IEnumerable<Telephone_Search.Models.tbl_users> users;
public IEnumerable<Telephone_Search.Models.tbl_pictures> images;

家庭控制器:

 [HttpPost]
    public ActionResult Index(HttpPostedFileBase file, MyViewModel model , tbl_pics pic)
    {
        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_content = file.FileName;
               pic.emp_no = 6;
               db.tbl_pictures.Add(pic);
               db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

         return this.View(new MyViewModel
        {
            images = pic //ERROR HERE
        });
    }

Razor 视图:

@foreach (var img in Model.images) {
        <img src="~/images/@img.profile_content" width="100" height="100"     />
        }
    @using (Html.BeginForm("Index", "Home", null, FormMethod.Post,
                      new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.picture_content)
        </div>
        <div class="editor-field">
            <input id="ImagePath" title="Upload a product image"
                   type="file" name="file" />
        </div>
        <p><input type="submit" value="Create" /></p>
    }

当我返回视图时,img 声明它不能隐式转换,因为存在显式覆盖? ,我的目标是在 tbl_pictures 中存储图像并返回一个视图模型,以便剃刀视图可以识别存储的内容 model.images?

假设 tbl_picstbl_pictures 相同,那么您遇到的问题似乎是您正试图将单个对象传递给 IEnumerable 在视图模型中。

而是创建一个新的 ArrayList<T> 或任何实现 IEnumerable 接口的东西,并用 pic 填充它并将其分配给 images 属性.

new MyViewModel
{
    images = new Telephone_Search.Models.tbl_pictures[] { pic };
}

感谢@Matias

,使用数组代替列表