谢谢解决方法 以下是您可以如何处理:
模型:
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",new Product { ID = 4,name = "product 4",}; 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>总结
以上是内存溢出为你收集整理的c# – 将复选框添加到MVCcontrib Grid上的每一行全部内容,希望文章能够帮你解决c# – 将复选框添加到MVCcontrib Grid上的每一行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)