我的问题是,当使用 Grid.Mvc 时,当我的 View 需要 aList<Client>
作为参数时,我的 View 返回 null 给我的控制器,但是当它需要 a 时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 查看源代码 (_Grid.cshtml) 更改foreach
为for
,但它并没有解决我的问题。