2011-02-04 5 views
2

입력하는 새 데이터를 게시하는 데 어려움이 있습니다. 제출하기 전에 데이터를 변경 했음에도 불구하고보기로 전송 된 데이터가 컨트롤러로 다시 전송 된 것으로 보입니다. 다음과 같이목록이 다시 게시 될 때 바인딩 어려움

내 코드는 다음과 같습니다

컨트롤러 공용 클래스 GroupRateController : 컨트롤러 // GET { // :/GroupRate/

public ActionResult Index() 
    { 
     GroupRateModel model = new GroupRateModel(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(GroupRateModel model) 
    { 
     model.Save(model); 
     return View(model); 
    } 

} 

보기

@model MvcApplication1.Models.GroupRateModel 

@{ 
    View.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 
@using (Html.BeginForm()) 
{ 

@Html.ValidationSummary() 

<table> 
<thead> 

</thead> 
<tr><th>Rate Group</th><th>Default Amount</th><th>Client Amount</th></tr> 
@foreach (var item in @Model.ClientRateDetails) 
{ 
<tr><td>@item.RateGroupName</td><td align="right">@Html.DisplayFor(m => @item.RateGroupID)</td><td>@Html.EditorFor(model => item.ClientRate)</td></tr> 

} 
</table> 

<p> <input type ="submit" value="Save" id="submit" /></p> 
} 

모델

using System.ComponentModel.DataAnnotations; 

namespace MvcApplication1.Models 
{ 
    public class GroupRateModel 
    { 
     public List<ClientRateDetailsModel> ClientRateDetails = new List<ClientRateDetailsModel>() ; 
     public string Name { get; set; } 


     public GroupRateModel() 
     { 
        ClientRateDetails.Add(new ClientRateDetailsModel 
        { 
         RateGroupID = 1, 
         RateGroupName = "Test1", 
         ClientRate = 100 
        }); 

        ClientRateDetails.Add(new ClientRateDetailsModel 
        { 
         RateGroupID = 2, 
         RateGroupName = "Test2", 
         ClientRate = 200 
        }); 

        ClientRateDetails.Add(new ClientRateDetailsModel 
        { 
         RateGroupID = 3, 
         RateGroupName = "Test3", 
         ClientRate = 300 
        }); 

     } 

     public void Save(GroupRateModel model) 
     { 
      foreach (var item in model.ClientRateDetails) 
       { 
        //...; 
       } 
      } 
     } 


    public class ClientRateDetailsModel 
    {  
     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:00.00}", NullDisplayText = "")] 
     [Range(0, (double)decimal.MaxValue, ErrorMessage = "Please enter a valid rate")] 
     public decimal? ClientRate { get; set; } 
     public int? RateGroupID { get; set; } 
     public string RateGroupName { get; set; } 
    } 

}

답변

5

모델 바인더가 제대로 값을 가져올 수 있도록 대한 입력 컨트롤의 이름이 올바른 이름이 없기 때문에이 될 수 있습니다. 또한 ClientRateDetails은 속성이 아니라 모델에 올바르게 바인딩되지 않은 필드가 있다는 것을 알았습니다. 모델과

시작 :

public class GroupRateModel 
{ 
    public IEnumerable<ClientRateDetailsModel> ClientRateDetails { get; set; } 
    public string Name { get; set; } 

    public GroupRateModel() 
    { 
     // Remark: You cannot assign your ClientRateDetails collection here 
     // because the constructor will be called by the default model binder 
     // in the POST action and it will erase all values that the user 
     // might have entered 
    } 

    public void Save(GroupRateModel model) 
    { 
     foreach (var item in model.ClientRateDetails) 
     { 
      //...; 
     } 
    } 
} 

public class ClientRateDetailsModel 
{  
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:00.00}", NullDisplayText = "")] 
    [Range(0, (double)decimal.MaxValue, ErrorMessage = "Please enter a valid rate")] 
    public decimal? ClientRate { get; set; } 
    public int? RateGroupID { get; set; } 
    public string RateGroupName { get; set; } 
} 

다음 컨트롤러 :

public class HomeController: Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new GroupRateModel(); 
     model.ClientRateDetails = new[] 
     { 
      new ClientRateDetailsModel 
      { 
       RateGroupID = 1, 
       RateGroupName = "Test1", 
       ClientRate = 100 
      }, 
      new ClientRateDetailsModel 
      { 
       RateGroupID = 2, 
       RateGroupName = "Test2", 
       ClientRate = 200 
      }, 
     }; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(GroupRateModel model) 
    { 
     model.Save(model); 
     return View(model);  
    } 
} 

후 해당보기 :

@model MvcApplication1.Models.GroupRateModel 
@{ 
    View.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<h2>Index</h2> 
@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary() 
    <table> 
    <thead> 
     <tr> 
      <th>Rate Group</th> 
      <th>Default Amount</th> 
      <th>Client Amount</th> 
     </tr> 
    </thead> 
    @Html.EditorFor(x => x.ClientRateDetails) 
    </table> 
    <p><input type ="submit" value="Save" id="submit" /></p> 
} 
을 그래서 여기 당신이 코드를 개선하기 위해 제안 방법

다음 해당 편집기 템플릿 (~/Views/Home/EditorTemplates/ClientRateDetailsModel.cshtml) :

@model MvcApplication1.Models.ClientRateDetailsModel 
<tr> 
    <!-- Make sure you include the ID as hidden field 
     if you want to get it back inside the POST action 
    --> 
    @Html.HiddenFor(x => x.RateGroupID) 
    <td>@Model.RateGroupName</td> 
    <td align="right">@Model.RateGroupID</td> 
    <td>@Html.EditorFor(x => x.ClientRate)</td> 
</tr> 
+0

덕분에 대단히 도움이되었습니다. – ozz

관련 문제