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

本文关键字:模型 返回 null 列表 Mvc 期望 Grid | 更新日期: 2023-09-27 17:56:05

我的问题是,当使用 Grid.Mvc 时,我的视图在期望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进行操作时,它可以工作,但在操作Test时返回null

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

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

因为您没有模型供您查看。在模型顶部添加类似@model IList<Client>的内容。