我对MVC非常非常新,所以请忍受我的问题,我必须使用以下结构。 我有以下型号,设施和地址。 Facility包含一个SortedList of Address,为了清晰起见,我减少了属性数量,
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"]);
}
查看: FacilityCreate
@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>
}
PartialView: 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>
我的问题是,在Controller中,objFacility.Addresses没有获得为模型AppAddress输入的值,它始终为null。然而,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>
请帮忙。
答案 0 :(得分:0)
我猜生成的HTML不适合模型绑定。
不应该是objFacility.Addresses[0].StreetName
而是Addresses[0].StreetName
。
答案 1 :(得分: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)
}
})