私はMVCに非常に慣れていないので、私の質問に耐えてください。次の構造で作業する必要があります。私は次のモデル、施設と住所を持っています。ファシリティにはアドレスのSortedListが含まれています。わかりやすくするために、プロパティの数を減らしました。
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>
私の質問は、コントローラーで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>
助けてください。