当期望列表时,Grid.Mvc向模型返回null

时间:2016-07-07 18:35:06

标签: c# asp.net asp.net-mvc grid.mvc

我的问题是,当使用Grid.Mvc时,我的View会在List<Client>作为参数时向我的控制器返回null,但是当它需要FormColletion时,它可以正常工作。

我尝试使用常规的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>
    }

使用有效的常规表的另一个视图是:

@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>
}

基本上,当我的HttpPost采取行动Salvar时会有效,但当行动为null时会返回Test

Obs:我尝试更改Grid.Mvc View源代码(_Grid.cshtml)以将foreach替换为for,但它并没有解决我的问题。

1 个答案:

答案 0 :(得分:0)

因为您没有为您查看模型。在模型上添加@model IList<Client>之类的内容。