Comment puis-je append une case à cocher à chaque ligne d’une grid MVCconsortingb? puis, lorsque le formulaire est posté, découvrez quels enregistrements ont été sélectionnés? Je ne trouve pas grand chose en cherchant cela. Je vous remercie
Voici comment vous pouvez procéder:
Modèle:
public class Product { public int Id { get; set; } public ssortingng Name { get; set; } public bool IsInStock { get; set; } }
Manette:
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"); } }
Vue:
<% using (Html.BeginForm()) { %> <%= Html.Grid(Model) .Columns(column => { column.For(x => x.Id); column.For(x => x.Name); column.For(x => x.IsInStock) .Partial("~/Views/Home/IsInStock.ascx"); }) %> <% } %>
Partiel:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Et enfin, voici une méthode d’assistance que vous pouvez utiliser pour générer cette case à cocher:
using System.Web.Mvc; using Microsoft.Web.Mvc; public static class HtmlExtensions { public static MvcHtmlSsortingng EditorForIsInStock(this HtmlHelper htmlHelper) { var tagBuilder = new TagBuilder("input"); tagBuilder.MergeAtsortingbute("type", "checkbox"); tagBuilder.MergeAtsortingbute("name", htmlHelper.NameFor(x => x.IsInStock).ToHtmlSsortingng()); if (htmlHelper.ViewData.Model.IsInStock) { tagBuilder.MergeAtsortingbute("checked", "checked"); } return MvcHtmlSsortingng.Create(tagBuilder.ToSsortingng()); } }
Ce qui simplifie le partiel:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <%: Html.EditorForIsInStock() %>
Je ne sais pas si cela aide, mais j’ai fait la même chose en utilisant le code ci-dessous:
@Html.Grid(Model.PagedModel).AutoGenerateColumns().Columns(column => { column.For(a => Html.ActionLink("Edit", "Edit", new { a.ID })).InsertAt(0).Encode(false); column.Custom(a => Html.Raw("" ) ); })
Mon contrôleur a ensuite pu recevoir les éléments de la liste de contrôle sélectionnés:
[HttpPost] public ViewResult List(ssortingng[] resubmit)
Pour résoudre ce problème dans chaque requête get, j’ajoute la valeur des cases à cocher sous forme de chaîne séparée par des virgules. Récupérez ensuite les valeurs de la chaîne de requête.
$("#pageLayout a").click(function () { //Check for the click event insed area pageLayout //get the href of link var link = $(this).attr('href'); var apps = ''; //for all the checkbox get the checked status $("input:checkbox").each( function () { if ($(this).attr('name') != 'IsChecked') { if (apps != '') { apps = apps + ',' + $(this).attr('name') + '=' + $(this).is(':checked'); } else { apps = $(this).attr('name') + '=' + $(this).is(':checked'); } } } ) //Used to check if request has came from the paging, filter or sorting. For the filter anchor href doesnt haave //any query ssortingng parameter. So for appending the parameter first ? mark is used followed by list of query ssortingng //parameters. var index = link.lastIndexOf('?'); //If there is no question mark in the ssortingng value return is -1 if (index == -1) { //url for the filter anchor tag //appList hold the comma sep pair of applicationcode=checked status link = link + '?appList=' + apps + '&profileName=' + $('#ProfileName').val(); } else { //url for sorting and paging anchor tag link = link + '&appList=' + apps + '&profileName=' + $('#ProfileName').val(); } //Alter the url of link $(this).attr('href', link); });
Dans votre vue (par exemple, ReportList.cshtml), incluez votre grid dans un formulaire et définissez une action pour le formulaire.
Ensuite, dans votre contrôleur, implémentez votre méthode d’action
public ActionResult SubmitReportList (ssortingng ReportListSubmit, IList chkSelected){ // The IList chkSelected will give you a list of values which includes report ids for the selected reports and “false” for the non-selected reports. You can implement your logic accordingly. return View("ReportList"); }