2016-07-07 4 views
0

내 문제는 Grid.Mvc를 사용할 때 List<Client>을 매개 변수로 사용할 때 내 뷰가 null을 내 컨트롤러로 반환하지만 FormColletion을 예상하면 올바르게 작동합니다.Grid.Mvc는 모델을 기대할 때 null을 반환합니다.

일반 html 테이블을 사용해 보았는데 제대로 작동합니다.

아래 코드는 제 코드입니다.

모델 :

public class Client 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public double Valor { get; set; } 
} 

컨트롤러 :

[HttpGet] 
    public ActionResult Index() 
    { 
     return View(clients); 
    } 

    [HttpGet] 
    public ActionResult Test() 
    { 
     return View(clients); 
    } 

    [HttpPost] 
    public ActionResult Salvar(FormCollection form) 
    { 
     var length = form.GetValues(form.AllKeys[0]).Length; 
     var obj = clients.ToDictionary(x => x.Id); 
     Client novo; 

     for (var j = 0; j < length-1; j++) 
     { 
      novo = new Client { Id = Convert.ToInt32(form.GetValues("id")[j]), 
           Name = form.GetValues("Name")[j], 
           Email = form.GetValues("Email")[j], 
           Valor = Convert.ToInt32(form.GetValues("Valor")[j]) }; 
      AtualizaCliente(novo); 
     } 

     return Redirect("/"); 
    } 

    [HttpPost] 
    public ActionResult Test(List<Client> clients) 
    { 

     clients.Name + " - " + clients.Email + " - " + clients.Valor); 

     for(int i = 0; i < clients.Count; i++) 
     { 
      System.Diagnostics.Debug.WriteLine(clients[i].Id + " - " + clients[i].Name + " - " + clients[i].Email + " - " + clients[i].Valor); 
     } 

     return Redirect("/"); 
    } 

보기 :

model List<WebApplication3.Models.Client> 
@using GridMvc.Html 

    @using (Html.BeginForm("Test", "Home", FormMethod.Post)){ 

    <div> 
     @Html.Grid(Model).Columns(columns => 
    { 
     columns.Add() 
     .Encoded(false) 
     .Sanitized(false) 
     .SetWidth("5%") 
     .RenderValueAs(c => Html.TextBox("Id", c.Id, new { @id = "Id", @readonly = "readonly", @class = "form-control input-sm"})) 
     .Titled("Client ID"); 
     columns.Add() 
     .Encoded(false) 
     .Sanitized(false) 
     .RenderValueAs(c => Html.TextBox("Name", c.Name, new { @id = "Name", @readonly= "readonly", @class= "form-control col-md-4 input-sm" })) 
     .Titled("Name") 
     .Filterable(true) 
     .Sortable(true); 
     columns.Add() 
     .Encoded(false) 
     .Sanitized(false) 
     .RenderValueAs(c => Html.TextBox("Email", c.Email, new { @id = "Email", @readonly = "readonly", @class = "form-control col-md-4 input-sm" })) 
     .Titled("Email"); 
     columns.Add() 
     .Encoded(false) 
     .Sanitized(false) 
     .SetWidth("10%") 
     .RenderValueAs(c => Html.TextBox("Valor", c.Valor, new { @id = "Valor", @class = "form-control col-md-2 input-sm" })) 
     .Titled("Valor").Filterable(true).Format("{0:c}"); 
    }).Sortable(true) 
    </div> 

    <div class="form-actions text-right pal"> 
     <button type="submit" class="btn btn-primary" name="Salvar" value="Salvar"> 
      Salvar Alterações 
     </button> 
    </div> 
    } 

다른 뷰를 사용하여 일반 t 작동 청정도는 내 HttpPost 행동 Salvar로 이동하지만 작업이 Testnull을 반환 할 때

@using (Html.BeginForm("Test", "Home", FormMethod.Post)) 
{ 
<table class="table"> 

    @for (int i = 0 ; i < Model.Count ; i++) 
    { 
     <tr> 
      <td> 
       @Html.HiddenFor(m => Model[i].Id) 
      </td> 
      <td> 
       @Html.TextBoxFor(m => Model[i].Name) 
      </td> 
      <td> 
       @Html.TextBoxFor(m => Model[i].Email) 
      </td> 
      <td> 
       @Html.TextBoxFor(m => Model[i].Valor) 
      </td> 
     </tr> 
    } 
</table> 

<div class="form-actions text-right pal"> 
    <button type="submit" class="btn btn-primary" name="Salvar" value="Salvar"> 
     Salvar Alterações 
    </button> 
</div> 
} 

기본적으로, 그것은 작동합니다.

것을 제외 : 나는 Grid.Mvc보기 소스 코드 (_Grid.cshtml가) forforeach을 대체하기 위해 변경했는데, 그것은 내 문제를 해결 돌아가신`t.

+1

ASP.NET Core MVC (올바른 태그를 사용하는 경우) 또는 ASP.NET Core MVC (MVC1 - MVC5)에 대한 질문입니까? 그런 다음 asp.net-mvc6 태그를 제거하십시오. 더 이상 mvc6이 없으며 이전 ASP.NET MVC 프레임 워크를 기반으로 한 차후 버전으로 예약되었습니다. – Tseng

답변

0

당신이보기에 적합한 모델이 아니기 때문에. 모델 상단에 @model IList<Client>과 같은 것을 추가하십시오.

+0

모델이 있지만 코드에서이를 표시하는 것을 잊어 버렸습니다. 지금 업데이트되었습니다. –

관련 문제