Вот как можно продолжить:
Модель:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsInStock { get; set; }
}
Контроллер:
public class HomeController : Controller
{
public ActionResult Index()
{
var products = new[]
{
new Product { Id = 1, Name = "product 1", IsInStock = false },
new Product { Id = 2, Name = "product 2", IsInStock = true },
new Product { Id = 3, Name = "product 3", IsInStock = false },
new Product { Id = 4, Name = "product 4", IsInStock = true },
};
return View(products);
}
[HttpPost]
public ActionResult Index(int[] isInStock)
{
// The isInStock array will contain the ids of selected products
// TODO: Process selected products
return RedirectToAction("Index");
}
}
Вид:
<% using (Html.BeginForm()) { %>
<%= Html.Grid<Product>(Model)
.Columns(column => {
column.For(x => x.Id);
column.For(x => x.Name);
column.For(x => x.IsInStock)
.Partial("~/Views/Home/IsInStock.ascx");
})
%>
<input type="submit" value="OK" />
<% } %>
Частичная:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.Models.Product>" %>
<!--
TODO: Handle the checked="checked" attribute based on the IsInStock
model property. Ideally write a helper method for this
-->
<td><input type="checkbox" name="isInStock" value="<%= Model.Id %>" /></td>
И, наконец, вот вспомогательный метод, который вы можете использовать для создания этого флажка:
using System.Web.Mvc;
using Microsoft.Web.Mvc;
public static class HtmlExtensions
{
public static MvcHtmlString EditorForIsInStock(this HtmlHelper<Product> htmlHelper)
{
var tagBuilder = new TagBuilder("input");
tagBuilder.MergeAttribute("type", "checkbox");
tagBuilder.MergeAttribute("name", htmlHelper.NameFor(x => x.IsInStock).ToHtmlString());
if (htmlHelper.ViewData.Model.IsInStock)
{
tagBuilder.MergeAttribute("checked", "checked");
}
return MvcHtmlString.Create(tagBuilder.ToString());
}
}
Что упрощает частичное:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.Models.Product>" %>
<td><%: Html.EditorForIsInStock() %></td>