如何处理“PartialRender"模型? [英] How to handle `PartialRender` Models?
问题描述
如果我碰巧有
公共类 DoorsModel{公共门模型(){}公共 HttpPostedFileBase 图像 { 获取;放;}公共字符串门布局{得到;放;}公共布尔 ReplicateSettings { 获取;放;}公共列表<DoorDesignModel>门{得到;放;}}公共类 DoorDesignModel{公共门设计模型(){}公共 HttpPostedFileBase FrontFile { 获取;放;}公共 HttpPostedFileBase BorderFile { 获取;放;}}
在我的 View
中,我有一个普通的表单来填充模型属性,但是 List
我正在使用用户控件并使用
<%Html.RenderPartial("DoorDesign", Model.Doors);%>
在 DoorDesign.ascx
我有:
<%@ 控制语言="C#" AutoEventWireup="true"Inherits="System.Web.Mvc.ViewUserControl<List<MyProject.Backend.Models.DoorDesignModel>>"%>
要显示所有表单,我有一个 for
子句
MyProject.Backend.Models.DoorDesignModel 字段;for (i = 0; i < Model.Count; i++) {字段=模型[i];...}
我正在使用 HTML
但很快我按下提交按钮,我的模型返回一个 null
列表:(我在启动视图时创建并设置一个新列表
public ActionResult Doors(){DoorsModel 模型 = 新 DoorsModel();model.Doors = new List();for (int i= 1; i<= 24; i++)//添加 24 扇门model.Doors.Add(新 DoorDesignModel());返回视图(模型);}[HttpPost]公共 ActionResult Doors(DoorsModel 模型){//model.Doors 始终为 null !!!如果(模型状态.IsValid)ViewData["General-post"] = "有效";别的ViewData["General-post"] = "NOT Valid";返回视图(模型);}
我需要什么才能从 RenderPartial
部分返回门列表?
一个简单的视图模型
刚遇到同样的问题.找到这个网站:http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspxp>
基本上都是关于<input type="file" value="上传文件" name="Doors[<%: i %>].FrontFile" id="Doors[<%: i %>].FrontFile">
if by any means I happen to have
public class DoorsModel
{
public DoorsModel() { }
public HttpPostedFileBase Image { get; set; }
public String DoorLayout { get; set; }
public bool ReplicateSettings { get; set; }
public List<DoorDesignModel> Doors { get; set; }
}
public class DoorDesignModel
{
public DoorDesignModel() { }
public HttpPostedFileBase FrontFile { get; set; }
public HttpPostedFileBase BorderFile { get; set; }
}
and in my View
I have a normal form to populate the Model Properties but the List<DoorDesignModel>
I'm using a User Control and use
<%Html.RenderPartial("DoorDesign", Model.Doors); %>
inside DoorDesign.ascx
I have:
<%@ Control
Language="C#" AutoEventWireup="true"
Inherits="System.Web.Mvc.ViewUserControl<List<MyProject.Backend.Models.DoorDesignModel>>" %>
to display all form I have a for
clause
MyProject.Backend.Models.DoorDesignModel field;
for (i = 0; i < Model.Count; i++) {
field = Model[i];
...
}
and I'm using the HTML
<input type="file" value="Upload file"
name="Doors.FrontFile[<%: i %>]" id="Doors.FrontFile[<%: i %>]">
but soon I press the submit button, my model returns a null
List :(
and I'm creating and setting a new list when starting the View as
public ActionResult Doors()
{
DoorsModel model = new DoorsModel();
model.Doors = new List<DoorDesignModel>();
for (int i= 1; i<= 24; i++) // Add 24 Doors
model.Doors.Add(new DoorDesignModel());
return View(model);
}
[HttpPost]
public ActionResult Doors(DoorsModel model)
{
// model.Doors is always null !!!
if (ModelState.IsValid)
ViewData["General-post"] = "Valid";
else
ViewData["General-post"] = "NOT Valid";
return View(model);
}
What do I need to have in order to return the Doors List from the RenderPartial
part?
a simple mockup of the View
Just had the same problem. Found this website: http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx
Essentially it is all about
<input type="file" value="Upload file" name="Doors[<%: i %>].FrontFile" id="Doors[<%: i %>].FrontFile">
这篇关于如何处理“PartialRender"模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!