2012-05-29 1 views
0

저는 MVC에 매우 익숙합니다. 제 질문에 답해주십시오. 다음 구조로 작업해야합니다. 다음 시설, 주소 및 모델이 있습니다.Partialview는 sortedlist에 대한 모델을 반환하지 않습니다.

[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> 

부분보기 시설이 주소의 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; } 
} 

컨트롤러를 감소 : 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 : 항상 널 (null) 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가 생성됩니다. 있는 HTML이 뒤에 p_CreateEditAddress

<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

어떤 이유로 당신이 objFacility 접두사를 설정하고 있는가 :

이가 (I 라인 읽기 쉽에서 휴식을 넣어) 시도? – VJAI

답변

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) 
     } 
    }) 
+0

답장을 보내 주셔서 감사합니다. 나는 당신이 이전에 행운을 빌어 서 언급 한 방식을 시도했다. – user1424052

+0

@ user1424052 아, 괜찮습니다. 흥미가 없으면 왜 'SortedList'입니까? 나는 그것이 정렬 목록을 색인하려고하는 문제 일 수 있다고 생각합니다 :). 'List'로 바꿀 수 있습니까? – mattytommo

+0

나중에 답장을 드려 죄송합니다. 모든 것을 List로 변경하면 완벽하게 작동합니다. 답장 해주셔서 매우 감사합니다. sortedlist가 작동하지 않는 이유에 대해 약간의 통찰력을 던져 줄 수 있습니까? – user1424052

0

생성 된 HTML이 모델 바인딩에 적합하지 않다고 생각합니다.

objFacility.Addresses[0].StreetName이 아니라 Addresses[0].StreetName이어야합니다.

+0

나는 그것을 행운과 함께 eariler로 시험해 보았다. 감사. – user1424052

+0
관련 문제