c# - Adding check boxes to each row on MVCcontrib Grid -


How can I add a checkbox to each row of the MVCcontrib grid? Then when the form is posted, it was discovered which record was selected? I can not find much when searching for this. Thanks

How can you proceed:

Model:

  Public Class Product {Public Entry ID (Received); Set; } Public string name {get; Set; } Receive Public Bull Isstock { Set; Controller: Public Sector HomeController: Controller {public workspace index} {var products = new [] {new product {id = 1}, name = "product1", isInStock = false}, new product {id = 2, name = " Product 2 ", IsInStock = true}, new product {id = 3, name =" product3 ", isInStock = false}, new product {id = 4, name =" product4 ", isInStock = true},}; See Return (Product); } [HTTOST] Public Action Result Index (Int [] Hainstock) / iiStock array will include the ID of the selected products / TODO: The process of selected products will be re-direct redaction ("index"); }}  

See:

  Use of <% (Html.BeginForm ()) {%> & Lt;% = Html.Grid & gt; Products & gt; (Model). (X = & gt; x.Name); column (X = & gt; x.IsInStock) for column (column = & gt; {column.for (x = & gt; x.Id);). Accessory ("~ / scene / home / isInStock.ascx");})%> & Lt; Input type = "submit" value = "ok" /> & Lt;%}% & gt;  

partial:

  & lt;% @ control language = "c #" inheritance = "system web.mvc.useUseus control  & Lt; Input type = "checkbox" name = "isInStock" value = "& lt;% = Model.id%>" / & Gt; & Lt; / TD & gt;  

And finally there is a helpful method you can use to generate this checkbox:

  Using System.Web.Mvc Tax; Using Microsoft.Web.Mvc; Public static class HtmlExtensions {public static MvcHtmlString EditorForIsInStock (this HtmlHelper & lt; product & gt; htmlHelper) {var Tag Creator / New Tag Creator ("Input"); Tag Builder Merge author ("type", "checkbox"); Tagbuilder.marge attribute ("name", htmlHelper.NameFor (x = & gt; x.IsInStock). ToHtmlString ()); If (htmlHelper.ViewData.Model.IsInStock) {tagBuilder.MergeAttribute ("check", "checked"); } Return MvcHtmlString.Create (tagBuilder.ToString ()); }}  

Which partial simplifies:

  <% @ Control Language = "C #" Inherits = "System.Web. Mvc.ViewUserControl & lt; MyNamespace .Models.Product & gt; "& Gt%; & Lt; Td> & Lt;%: Html.EditorForIsInStock ()%> & Lt; / Td>  

Comments