オブジェクトのコレクションを表示するために ASP.NET WebGrid に取り組んでおり、コレクションに新しいオブジェクトを追加できるようにしています。
Webgrid のビューには、テンプレートに基づいて新しい行を動的に追加し、グリッドに追加する [追加] ボタンがあります。
助けが必要だ
1) クライアント側で新しい行を WebGrid モデルにバインドする方法。2) モデルで定義された DataAnnotations 検証を使用して WebGrid で検証を有効にする方法。
以下はコードです。
public class TestObject
{
public virtual int Id
{
get;
set;
}
[Required(ErrorMessageResourceName = "Required")]
[Display( Name = "Codee")]
public virtual string Code
{
get;
set;
}
[Required(ErrorMessageResourceName = "Required")]
[Display( Name = "Description")]
public virtual string Description
{
get;
set;
}
}
索引.html
@using (Html.BeginForm("Index", "Test", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary()
<div class="body-container">
<table class="form-grid">
<tr>
<td>
<input type="button" onclick="AddRow(); return false;">
</td>
</tr>
</table>
<table class="form-grid">
<tr>
<td>
<div id="partialregion">
@Html.Partial("_ListView", Model)
</div>
</td>
</tr>
</table>
<table id="newTemplate" style="display: none;">
<tr class="item">
<td>
<input type="hidden" id="cId" /></td>
<td>
<input type="text" id="code" />
</td>
<td>
<input type="text" id="description" />
</td>
</tr>
</table>
</div>
_ListView.HTML
@model IEnumerable<TestObject>
<div id="testGrid">
@{
var grid = new WebGrid(null, ajaxUpdateContainerId: "testGrid" , canSort: false);
grid.Bind(Model);
@MvcHtmlString.Create(
@grid.GetHtml(
tableStyle: "grid-table",
columns: grid.Columns(
grid.Column("",header:"",format : @<input id="hiddenId" type="hidden" value="@item.Id" />),
grid.Column("Code", header: "Code", format: @<input type="text" id="mCode" value="@item.Code" />),
grid.Column("Description", header: "Description", format: @<input type="text" id="mDesc" value="@item.Description" />)
)).ToString())
}
</div>