Model 包含模型列表的模型(MVC-3,Razor)
这个问题已经困扰我两天了。也有一些类似的帖子,但没有一个能完全解决我的问题 使用MVC-3,Razor语法: --EDIT.cshtml-- --MyMainModel.cs-- 那有什么问题?嗯,当按下“Complete Edit!”按钮时,MyController的编辑被调用,正如所有MyMainModle的数据在tact中所预期的那样。。。。除了ThumbnailModel的列表之外,这些都是空的 这应该怎么做?我尝试了许多不同的方法,包括制作一个可编辑的模板和使用EditFor(o=>…都没有用(这变得很混乱,因为我不知道EditFor应该用于整个集合还是集合中的一个项目-我尝试了两种方法)。在我添加删除复选框的复杂性之前,所有这些都可以正常工作,因此需要检索ThumbnailModels列表以检查内部Delete属性值 感谢大家阅读并试图理解这一点Model 包含模型列表的模型(MVC-3,Razor),model,asp.net-mvc-3,razor,Model,Asp.net Mvc 3,Razor,这个问题已经困扰我两天了。也有一些类似的帖子,但没有一个能完全解决我的问题 使用MVC-3,Razor语法: --EDIT.cshtml-- --MyMainModel.cs-- 那有什么问题?嗯,当按下“Complete Edit!”按钮时,MyController的编辑被调用,正如所有MyMainModle的数据在tact中所预期的那样。。。。除了ThumbnailModel的列表之外,这些都是空的 这应该怎么做?我尝试了许多不同的方法,包括制作一个可编辑的模板和使用EditFor(o=>…
[免责声明-一些变量和方法名称已被更改,以保护无辜的程序。许多代码已被删除并替换为注释代码。]以下是我用来说明一些概念的示例: 型号:
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; }
}
视图:
这不应该是
thumbnail=>thumbnail.Delete
?太好了!非常感谢您-这非常有用。我尝试使用这个示例的最后一件事是,我对每个thumbnailmodel都有一个稍微复杂的布局。在我的原始代码中,我使用了类似@foreach的东西(Model.ThumbnailImagePathAndNames中的var thumbnail)…似乎我不能在编辑和之后使用@Html.EditorFor(x=>thumbnail)来编辑布局(列限制)代码。你知道我怎样才能得到这种细粒度的控制吗?在模板中,不知何故将@model IEnumerable放在顶部?谢谢!**我可能必须将上述问题转移到一个新的线程中。换言之,我希望对@Html.EditorFor有更多的控制,使用我自己的@Foreach单独处理每个模型,而不是让.NET循环通过nd自动渲染。更改编辑器模板以获取@model IEnumerable并在其中实现@foreach被证明是不成功的。@Rob,我不明白您需要什么样的控件。在您的示例中,您只是在集合中循环,并为每个元素输出两个输入字段。EditorFor应该是这足以避免您编写所有这些。嗨,Darin,最初(代码被剥离以简化示例),我一直在跟踪列数和行数,并根据我在@Foreach中的行数和列数更改DIV样式。但是,我发现这不是最好的解决方案,并决定对DIV采取稍微不同的方法。现在,我的格式与@Html.EditorFor(x=>x.ThumbnailImagePathAndNames)的预期效果一样您在上面概述了……因此,我的代码更加清晰易懂。再次感谢您花时间解释这一点!清晰的代码总是有助于解决与原始问题类似的问题。谢谢@DarinDimitrov
[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);
}
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));
}
}
public class ThumbnailModel
{
public Guid ImageGUID { get; set; }
public String FullSizePicturePath { get; set; }
public String ThumbnailPicturePath { get; set; }
public bool Delete { get; set; }
}
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!" />
}
@model AppName.Models.ThumbnailModel
<!-- Pass the image id as hidden field -->
@Html.HiddenFor(x => x.ImageGUID)
@Html.CheckBoxFor(x => x.Delete)