0

我对 MVC 非常非常陌生,所以请耐心等待我的问题,我必须使用以下结构。我有以下型号,设施和地址。设施包含地址的排序列表,为了清楚起见,我减少了属性的数量,

public class AppFacility
{
    public AppFacility()
    {
        this.Addresses = new SortedList<string, AppAddress>();
    }

    public SortedList<string, AppAddress> Addresses { get; set; }

    [DisplayNameAttribute("Facility ID")]
    public int FacilityID { get; set; }

    [Required(ErrorMessage = "Facility Name is a required field")]
    [DisplayNameAttribute("Facility Name")]
    public string FacilityName { get; set; }

    [DisplayNameAttribute("Doing Business As")]
    public string Dba { get; set; }

    [DisplayNameAttribute("Nbr. Of Employees")]
    public int NbrOfEmployees { get; set; }
}

public class AppAddress
{
    [DisplayNameAttribute("City")]
    public string City { get; set; }

    [DisplayNameAttribute("State")]
    public string State { get; set; }

    [DisplayNameAttribute("Street Name")]
    public string StreetName { get; set; }
 } 

控制器:

[HttpPost]        
    public ActionResult FacilityCreate(AppFacility objFacility)
    {
        facilityManager = new Manager.AppFacilityManager();

            if (facilityManager.InsertAppFacility(objFacility))
            {
                return RedirectToAction("FacilityInfo", new { id = objFacility.FacilityID });
            }

        return View((AppFacility)Session["FacilityObject"]);
    }

视图:设施创建

@model Model.CORE.BO.AppFacility
<table width="100%" align="center" border="0" class="SectionTables">
<tr>
    <td class="SubtitleHeader">
        Facility
    </td>
</tr>
<tr>
    <td class="SubtitleHeader1">
        Enter Facility Information
    </td>
</tr>
<tr>
    <td align="center">
        @Html.Partial(p_CreateEditAppFacility, Model)         
    </td>         
</tr>

部分视图:p_CreateEditAppFacility:这也包含 p_CreateEditAddress 部分视图。

@model Model.CORE.BO.AppFacility 
@using (Html.BeginForm("FacilityCreate", "Form", FormMethod.Post, 
new { id = "saveForm",     name = "saveForm" }))
{      
@Html.ValidationSummary(true)
<div class="main">
    <fieldset>
        <legend>Operator Information</legend>
        <div class="editor-label">
            @Html.Label("Facility Name (Business Name of Operator to Appear): ")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FacilityName)
            @Html.ValidationMessageFor(model => model.FacilityName)
        </div>
        <div class="editor-label">
            @Html.Label("Owner's Business Name (If different from Business Name of Operator): ")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Dba)
            @Html.ValidationMessageFor(model => model.Dba) 
        </div>
        <div class="editor-label">
            @Html.Label("No of Employees:")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.NbrOfEmployees)
            @Html.ValidationMessageFor(model => model.NbrOfEmployees)
        </div>
    </fieldset>

    <fieldset>
        <legend>Address Information</legend> 
       @{ 
int i = 0;
foreach (KeyValuePair<string, Model.CORE.BO.AppAddress> addressRow in Model.Addresses)
{

        <div class="editor-field">
        @Html.Partial(p_CreateEditAddress, addressRow.Value, new ViewDataDictionary(Html.ViewDataContainer.ViewData) { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = string.Format("objFacility.Addresses[{0}]", i) } })       
        </div>
    i++;
}
    }
  </fieldset>

    <p>            
        <input id="SaveFacility" name="SaveInfo" type="submit" value="Save Information" />
    </p>
</div>

}

部分视图:p_CreateEditAddress

@model Model.CORE.BO.AppAddress
@Html.ValidationSummary(true)

<div class="editor-label">
    @Html.LabelFor(model => model.StreetName)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.StreetName)
    @Html.ValidationMessageFor(model => model.StreetName)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.City)
    @Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.State)
    @Html.ValidationMessageFor(model => model.State)
</div>

我的问题是,在控制器中,objFacility.Addresses 没有获取为模型 AppAddress 输入的值,它始终为空。AppFacility 被填充了。p_CreateEditAddress 后面的 html 看起来像这样

<div class="editor-field">
    <input class="text-box single-line" 
id="objFacility_Addresses_0__StreetName"  
name="objFacility.Addresses[0].StreetName" type="text" value="" />
    <span class="field-validation-valid" 
data-valmsg-for="objFacility.Addresses[0].StreetName" data-valmsg-replace="true"></span>
</div>

请帮忙。

4

2 回答 2

0

您只需要更改您的部分视图调用以获取正确的前缀。您不给它模型名称,因为默认模型绑定器将创建您的类的实例并查找与项目名称匹配的字段Request.Form集合中项目名称匹配的字段。它对您为模型提供的变量名称一无所知,只知道模型类的属性。

试试这个(我把换行符放在可读性中):

@Html.Partial(p_CreateEditAddress, addressRow.Value,
    new ViewDataDictionary(Html.ViewDataContainer.ViewData) {
        TemplateInfo = new System.Web.Mvc.TemplateInfo { 
            HtmlFieldPrefix = string.Format("Addresses[{0}]", i) 
        } 
    })
于 2012-05-29T17:28:47.183 回答
0

我猜生成的 HTML 不适合模型绑定。

不应该是objFacility.Addresses[0].StreetName但是Addresses[0].StreetName

于 2012-05-29T17:13:04.950 回答