2012-09-12 2 views
1

나는 만들려고 할 때 매우 단순하지만 내 마음 속에는 단순하게 만들고 싶습니다. MVC ASP.NET 환경에서 하나의 모델을 만들지 만 여러 번 렌더링하려고합니다. 그것은 작동하지만 내가 다시 데이터를 얻으려고하면 나는 아무것도 없다. 이 같은 모델의 모양을동일한 모델, 한 번에 여러 번보기 ASP.NET MVC

public class HardwareModel 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    ... 
} 

래퍼의 모습 :

public class WrapperModel 
{ 
    public List<HardwareModel> HardwareList { get; set; } 

    public WrapperModel() 
    { 
     HardwareList = new List<HardwareModel>(); 
    } 
} 

컨트롤러 :

readonly Hardware _hardware = new Hardware(); 
    [Authorize] 
    public ActionResult Index() 
    { 
     return View(_hardware.GetHardwareList(int.Parse(Session["idEmployee"].ToString()))); 
    } 

    [HttpPost] 
    [Authorize] 
    public ActionResult Index(WrapperModel model) 
    { 
     return View(model); 
    } 

그리고보기 : 그래서

<% using (Html.BeginForm()) 
    {%> 
<% foreach (var hardware in Model.HardwareList) 
    {%>  
    <tr> 
     <td> 
      <div class="editor-label"> 
       <%: Html.LabelFor(m => hardware.SelectedHardwareType) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.DropDownListFor(m => hardware.SelectedHardwareType, hardware.Hardwaretypes) %> 
      </div> 
... 

결과 일부는 View Result

렌더링은 정확히 원하는 것이지만 문제는 내가 저장 버튼을 누르면 제어기의 두 번째 부분이 사용되지만 "WrapperModel model"의 값은 빈 List입니다. Request 값에서 모든 것이 컨트롤러로 보내지지만 WrapperModel에서는 일치하지 않는다는 것을 알 수 있습니다.

"HardwareModel"의 수가 0 또는 99 일 수 있기 때문에 어떻게해야할지 모르겠으므로 HardwareModel1, HardwareModel2 ...을 (를) 만들 수 없습니다. 웹에서 읽은 것처럼 말입니다.

긴 게시물을 보내 주셔서 감사 드리며 죄송합니다.

답변

2

사용 EditorTemplate에 관심이있을 수 있습니다 그리고 당신은 좋은 것 : 나는 그것이 도움이되기를 바랍니다.

이 이름으로 AA보기합니다 (editortemplate를) EditorTemplates라는 폴더를 생성하는 Nd 만들기 HardwareModel.cshtml

enter image description here

이제 편집기 템플릿에 아래의 코드 (새보기)를 추가합니다. 요구 사항에 따라 레이아웃을 업데이트 할 수 있습니다.

@model HardwareModel   
<p> 
Name @Html.TextBoxFor(x => x.Name) <br /> 
Desc : @Html.TextBoxFor(x => x.Description) 
@Html.HiddenFor(x => x.Id) 
</p> 

이제 기본보기에서 Html.EditorFor HTML 도우미 메서드를 사용하여이 편집기 템플릿을 호출하십시오.

@model WrapperModel 
@using (Html.BeginForm()) 
{  
    @Html.EditorFor(x=>x.HardwareList) 
    <input type="submit" value="Save" />  
} 

양식의 하위 속성을 얻을 것이다 HttpPost 액션 메소드 제출하면 지금이

public ActionResult Hardware() 
{ 
    WrapperModel model = new WrapperModel(); 
    //HardCoded for demo.Can be replaced with entries based on your DB data 
    model.HardwareList.Add(new HardwareModel { Id=1, Name = "Printer", 
                Description = "desc" }); 
    model.HardwareList.Add(new HardwareModel { Id=2, Name = "Scanner", 
                Description = " desc2" }); 
    return View(model); 
} 

처럼보기에 HardwareModels의 목록을 전송하는 GET 작업을 가정

[HttpPost] 
public ActionResult Hardware(WrapperModel model) 
{ 
    //check for model.HardwareList Property here; 
    return View(model); 
} 

enter image description here

Here downlo 광고 귀하의 질문을 해결하기 위해 쓴 샘플.

+0

응답 해 주셔서 감사 드리며, 지금 당장이 작업을 수행하고 추가 할 부분을 확인하려고했습니다. –

+0

이 작품은 대단히 고마워. 내 MVC2 아키텍처 용 Razor synthax를 변형 시켰으며 모든 것이 잘 작동합니다. –