如何处理'PartialRender`模型?

时间:2010-11-02 18:21:02

标签: asp.net-mvc-2 user-controls viewmodel

如果无论如何我碰巧有

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列表:( 我在启动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);
}

为了从RenderPartial部分返回门列表,我需要具备哪些内容?

View的简单模型

alt text

1 个答案:

答案 0 :(得分:1)

刚遇到同样的问题。找到这个网站:http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx

基本上就是这一切 <input type="file" value="Upload file" name="Doors[<%: i %>].FrontFile" id="Doors[<%: i %>].FrontFile">