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

前端之家收集整理的这篇文章主要介绍了包含模型列表的模型(MVC-3,Razor)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题一直困扰我两天了。有一些类似的帖子,但没有一个完全解决我的问题。

使用MVC-3,Razor语法:

– EDIT.cshtml –

  1. @using (Html.BeginForm("Edit","My",FormMethod.Post,new { enctype = "multipart/form-data" }))
  2. {
  3. <!-- Some fields... -->
  4. <div class="editor-field">
  5. @Html.TextAreaFor(m => m.LongDescription)
  6. @Html.ValidationMessageFor(m => m.LongDescription)
  7. </div>
  8.  
  9. <!-- Some more fields work... Including picture upload (summary).-->
  10. <input name="button" type="submit" value="Add Picture" />
  11.  
  12. <!-- Picture Item display -->
  13. @foreach(var thumbnail in Model.ThumbnailImagePathAndNames)
  14. {
  15. <img src="@Url.Content(@thumbnail.ThumbnailPicturePath)" alt="" width="200" />
  16. @Html.RadioButtonFor(o=>o.SelectedImage,@thumbnail.ImageGUID) Primary Picture
  17. <!-- CheckBox to mark for deletion -->
  18. @Html.CheckBoxFor(o=>thumbnail.Delete) Delete ???????? <!---- Here is a problem - I don't understand how this should work -->
  19. }
  20. <input id="Submit1" name="button" type="submit" value="Complete Edit!" />
  21. }

– MyController.cs –

  1. [HttpPost]
  2. public ActionResult Edit(String button,HttpPostedFileBase file,MyMainModel model)
  3. {
  4. // if button = submit picture,work with picture here and break(long story)
  5.  
  6. // save model data
  7. // if valid,save and redirect
  8.  
  9.  
  10. // not valid or error,load up view like normal but with error messages
  11. model.LoadThumbnails();
  12. return View(model);
  13.  
  14. }

– MyMainModel.cs –

  1. public class MyMainModel
  2. {
  3. // some properties...
  4. public Guid? SelectedImage { get; set; }
  5.  
  6. [Display(Name = "Detailed Description")]
  7. public String LongDescription { get; set; }
  8.  
  9. // some more properties....
  10.  
  11.  
  12. // and finally my list of models
  13. public IList<ThumbnailModel> ThumbnailImagePathAndNames { get; set; }
  14.  
  15. public void LoadThumbnails()
  16. {
  17. // load up initial thumbnail models
  18. this.ThumbnailImagePathAndNames = new List<ThumbnailModel>(readDataService.GetThumbnailModels(this.SomeID));
  19. }
  20. }

– ThumbnailModels.cs –

  1. public class ThumbnailModel
  2. {
  3. public Guid ImageGUID { get; set; }
  4. public String FullSizePicturePath { get; set; }
  5. public String ThumbnailPicturePath { get; set; }
  6.  
  7. public bool Delete { get; set; }
  8. }

所以有什么问题?好了,当“完成编辑!按钮,MyController的编辑被调用,如所期望的所有MyMainModle的数据在tact ….除了ThumbnailModel的列表 – 那些结果是null。

这应该怎么做?我尝试了许多不同的方法包括使一个可编辑的模板和使用EditFor(o => …所有无法使用(这变得混乱,因为我不知道EditFor是否应该是整个集合或只是在集合中的一个项目 – 我尝试了两种方式)。所有使用的工作,直到我添加复选框的删除,因此需要检索ThumbnailModels列表检查内部Delete属性值。

谢谢大家的阅读和试​​图理解这一点。

[免责声明 – 一些变量和方法名称已更改以保护无辜程序。很多代码已经被删除,并替换为注释代码。]

解决方法

这里有一个例子,我用来说明一些概念:

模型:

  1. public class MyMainModel
  2. {
  3. public Guid? SelectedImage { get; set; }
  4. public string LongDescription { get; set; }
  5.  
  6. public IEnumerable<ThumbnailModel> ThumbnailImagePathAndNames { get; set; }
  7.  
  8. public HttpPostedFileBase File { get; set; }
  9. }
  10.  
  11. public class ThumbnailModel
  12. {
  13. public Guid ImageGUID { get; set; }
  14. public bool Delete { get; set; }
  15. }

控制器:

  1. public class HomeController : Controller
  2. {
  3. public ActionResult Index()
  4. {
  5. var model = new MyMainModel
  6. {
  7. // TODO: fetch from the repository instead of hardcoding
  8. ThumbnailImagePathAndNames = new[]
  9. {
  10. new ThumbnailModel { ImageGUID = Guid.NewGuid() },new ThumbnailModel { ImageGUID = Guid.NewGuid() },}
  11. };
  12. return View(model);
  13. }
  14.  
  15. [HttpPost]
  16. public ActionResult Index(MyMainModel model)
  17. {
  18. ... the model will be properly bound here
  19. }
  20. }

视图:

  1. @model AppName.Models.MyMainModel
  2. @{
  3. ViewBag.Title = "Index";
  4. Layout = "~/Views/Shared/_Layout.cshtml";
  5. }
  6. @using (Html.BeginForm("index","home",new { enctype = "multipart/form-data" }))
  7. {
  8. <div class="editor-field">
  9. @Html.TextAreaFor(m => m.LongDescription)
  10. @Html.ValidationMessageFor(m => m.LongDescription)
  11. </div>
  12. <input type="file" name="file" />
  13. <!-- Use different names for the upload and complete submit
  14. buttons so that you can distinguish which one was clicked
  15. in the POST action
  16. -->
  17. <input name="upload" type="submit" value="Add Picture" />
  18.  
  19. @Html.EditorFor(x => x.ThumbnailImagePathAndNames)
  20. <input name="complete" type="submit" value="Complete Edit!" />
  21. }

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

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

猜你在找的asp.Net相关文章