【发布时间】:2010-11-02 18:21:02
【问题描述】:
如果我碰巧有
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; }
}
在我的View 中,我有一个正常的表单来填充模型属性,但List<DoorDesignModel> 我正在使用用户控件并使用
<%Html.RenderPartial("DoorDesign", Model.Doors); %>
在DoorDesign.ascx里面我有:
<%@ Control
Language="C#" AutoEventWireup="true"
Inherits="System.Web.Mvc.ViewUserControl<List<MyProject.Backend.Models.DoorDesignModel>>" %>
要显示所有表单,我有一个 for 子句
MyProject.Backend.Models.DoorDesignModel field;
for (i = 0; i < Model.Count; i++) {
field = Model[i];
...
}
我正在使用 HTML
<input type="file" value="Upload file"
name="Doors.FrontFile[<%: i %>]" id="Doors.FrontFile[<%: i %>]">
但很快我按下提交按钮,我的模型返回一个null 列表:(
我在启动视图时创建并设置了一个新列表
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);
}
我需要什么才能从RenderPartial 部分返回门列表?
一个简单的视图模型
【问题讨论】:
-
1.您是否创建了 DoorsModel 模型活页夹? 2. 您是否将 From enctype 设置为“multipart/form-data”?
-
@Bivoauc 1. 是的,您可以在用户控件中将其视为继承我的模型 - 2. 是的,我可以在用户控件之外使用
Image,并且效果很好。
标签: asp.net-mvc-2 user-controls viewmodel