私の問題は、Grid.Mvc を使用している場合、ビューがList<Client>
as パラメーターを期待するときにコントローラに 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
、問題は解決しませんでした。