包含模型列表的模型 (MVC-3,Razor)

29

这个问题困扰我已经两天了。有一些类似的帖子,但没有一个完全解决我的问题。

使用MVC-3,Razor语法:

-- EDIT.cshtml --

@using (Html.BeginForm("Edit", "My", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <!-- Some fields... -->
    <div class="editor-field">
        @Html.TextAreaFor(m => m.LongDescription)
        @Html.ValidationMessageFor(m => m.LongDescription)
    </div>

    <!-- Some more fields work... Including picture upload (summary).-->
    <input name="button" type="submit" value="Add Picture" />

    <!-- Picture Item display -->
    @foreach(var thumbnail in Model.ThumbnailImagePathAndNames) 
    {
      <img src="@Url.Content(@thumbnail.ThumbnailPicturePath)" alt="" width="200" />
      @Html.RadioButtonFor(o=>o.SelectedImage, @thumbnail.ImageGUID)  Primary Picture 
      <!-- Checkbox to mark for deletion -->
      @Html.CheckBoxFor(o=>thumbnail.Delete) Delete ???????? <!---- Here is a problem - I don't understand how this should work -->
    }
    <input id="Submit1" name="button" type="submit" value="Complete Edit!" />
}

-- 我的控制器.cs --

 [HttpPost]
 public ActionResult Edit(String button, HttpPostedFileBase file, MyMainModel model)
 {
     // if button = submit picture,  work with picture here and break(long story)

     // save model data
         // if valid, save and redirect


     // not valid or error, load up view like normal but with error messages
     model.LoadThumbnails();
     return View(model);

 }

-- MyMainModel.cs --

public class MyMainModel
{
    // some properties...
     public Guid? SelectedImage { get; set; }

    [Display(Name = "Detailed Description")]
    public String LongDescription { get; set; }

    // some more properties....


    // and finally my list of models
    public IList<ThumbnailModel> ThumbnailImagePathAndNames { get; set; }

    public void LoadThumbnails()
    {
         // load up initial thumbnail models
         this.ThumbnailImagePathAndNames = new List<ThumbnailModel>(readDataService.GetThumbnailModels(this.SomeID));
    }
}

-- 缩略图模型.cs --

public class ThumbnailModel
{
    public Guid ImageGUID { get; set; }
    public String FullSizePicturePath { get; set; }
    public String ThumbnailPicturePath { get; set; }

    public bool Delete { get; set; }
}

那么问题出在哪里呢?当按下“Complete Edit!”按钮时,MyController的Edit被调用,如预期地保留了所有MyMainModel的数据……除了ThumbnailModel列表 - 它们变成了null。
应该如何解决这个问题?我尝试了许多不同的方法,包括制作可编辑模板和使用EditFor(o=>...,但都没有成功(这变得很困惑,因为我不知道EditFor是针对整个集合还是仅针对集合中的单个项 - 我两种方式都尝试了)。所有这些方法在添加复选框以进行删除时仍然有效,因此需要检索ThumbnailModels列表以检查内部Delete属性值。
感谢您的阅读和尝试理解。
[免责声明 - 一些变量和方法名称已更改以保护无辜程序。许多代码已被剥离并替换为注释代码。]

那不应该是 thumbnail=>thumbnail.Delete 吗? - Buildstarted
1个回答

45

这里有一个我准备好的示例来说明一些概念:

模型:

public class MyMainModel
{
    public Guid? SelectedImage { get; set; }
    public string LongDescription { get; set; }

    public IEnumerable<ThumbnailModel> ThumbnailImagePathAndNames { get; set; }

    public HttpPostedFileBase File { get; set; }
}

public class ThumbnailModel
{
    public Guid ImageGUID { get; set; }
    public bool Delete { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyMainModel
        {
            // TODO: fetch from the repository instead of hardcoding
            ThumbnailImagePathAndNames = new[] 
            {
                new ThumbnailModel { ImageGUID = Guid.NewGuid() },
                new ThumbnailModel { ImageGUID = Guid.NewGuid() },
                new ThumbnailModel { ImageGUID = Guid.NewGuid() },
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyMainModel model) 
    {
        ... the model will be properly bound here
    }
}

视图:

@model AppName.Models.MyMainModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("index", "home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="editor-field">
        @Html.TextAreaFor(m => m.LongDescription)
        @Html.ValidationMessageFor(m => m.LongDescription)
    </div>
    <input type="file" name="file" />
    <!-- Use different names for the upload and complete submit
         buttons so that you can distinguish which one was clicked
         in the POST action 
    -->
    <input name="upload" type="submit" value="Add Picture" />

    @Html.EditorFor(x => x.ThumbnailImagePathAndNames)    
    <input name="complete" type="submit" value="Complete Edit!" />
}

编辑器模板: (~/Views/Home/EditorTemplates/ThumbnailModel.cshtml):

@model AppName.Models.ThumbnailModel
<!-- Pass the image id as hidden field -->
@Html.HiddenFor(x => x.ImageGUID)
@Html.CheckBoxFor(x => x.Delete)

太好了!它起作用了!!非常感谢 - 非常有帮助。我最后一件想要在这个示例中工作的事情是,我对于每个ThumbnailModels都有一个更复杂的布局。在我的原始代码中,我使用类似@foreach(var thumbnail in Model.ThumbnailImagePathAndNames)...的东西...看起来我不能将我的布局(列限制等)代码放在编辑中,然后使用@Html.EditorFor(x => thumbnail)。你知道我怎么能获得这种细粒度的控制吗?在模板中以某种方式放置@model IEnumerable<SVS.Models.ThumbnailModel>在顶部?谢谢! - Rob
我可能需要将上面的问题移动到一个新的线程中。换句话说,我想更多地控制@Html.EditorFor,使用自己的@Foreach分别处理每个模型,而不是让.NET自动循环渲染它。将编辑器模板更改为接受@model IEnumerable<SVS.Models.ThumbnailModel>并在其中实现@foreach已经被证明是不成功的。 - Rob
@Rob,我不明白你需要什么样的控制。在你的示例中,你只是循环遍历集合,并为每个元素输出一些输入字段。EditorFor 应该足以避免你编写所有这些内容。 - Darin Dimitrov
嗨Darin,最初(代码被简化了),我一直在跟踪列和行计数,并根据我在@Foreach中所在的行和列更改DIV样式。然而,我发现这不是最好的解决方案,决定采用稍微不同的DIV方法。现在,我的格式化与您上面概述的@Html.EditorFor(x => x.ThumbnailImagePathAndNames)一样预期...因此,我的代码更加清晰易懂。再次感谢您抽出时间来解释这个问题! - Rob
清晰的代码总是有助于解决与原始问题类似的问题。谢谢@DarinDimitrov - Oscar Ortiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接