2010-06-19 40 views

Trả lời

9

Dưới đây là cách bạn có thể tiến hành:

mẫu:

public class Product 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool IsInStock { get; set; } 
} 

Bộ điều khiển:

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"); 
    } 
} 

Xem:

<% 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" /> 
<% } %> 

từng phần:

<%@ 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> 

Và cuối cùng, đây là một phương pháp helper bạn có thể sử dụng để tạo hộp kiểm này:

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()); 
    } 
} 

nào đơn giản hoá một phần:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.Models.Product>" %> 
<td><%: Html.EditorForIsInStock() %></td> 
+1

Rất đẹp, tôi sẽ cung cấp cho một thử này. Cảm ơn bạn! – twal

+0

@Darin Dimitrov Có thể sử dụng một đối tượng phức tạp trong POST thay vì một mảng các số nguyên không? Xem câu trả lời của bạn: http://stackoverflow.com/questions/5284395/checkboxlist-in-mvc3-view-and-get-the-checked-items-passed-to-the-controller – Rookian

+0

@Rookian, tất nhiên là có thể . –

0

Đối với việc giải quyết này trong mọi nhận được yêu cầu tôi các hộp kiểm được thêm vào có giá trị dưới dạng chuỗi được phân tách bằng dấu phẩy. Sau đó, lấy các giá trị từ chuỗi truy vấn.

$("#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 string parameter. So for appending the parameter first ? mark is used followed by list of query string 
     //parameters. 
     var index = link.lastIndexOf('?');    

     //If there is no question mark in the string 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); 


    }); 
3

Dont biết nếu điều này giúp nhưng tôi đã làm một điều tương tự bằng cách sử dụng mã bên dưới:

@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("<input type='checkbox' name='resubmit' value='" + a.ID + "'/>")); 
}) 

điều khiển của tôi là sau đó có thể nhận được các mục CheckBoxList chọn:

[HttpPost] 
public ViewResult List(string[] resubmit) 
+0

Đây là một giải pháp nhanh chóng và bẩn thỉu. Nhưng đôi khi đó là tất cả những gì bạn cần :) – th1rdey3

0

Trong chế độ xem của bạn (nói ReportList.cshtml) bao gồm lưới của bạn trong biểu mẫu và xác định hành động cho biểu mẫu

<html> 
<form action="/SubmitReportList"> 
      @{Html.Grid((List<NetSheet.Models.Report>)ViewData["ReportList"]) 
     .Sort((GridSortOptions)ViewData["sort"]) 
     .Attributes(id => "grid", @class => "grid") 

     .Columns(column => 
     { 
      column.For(c => Html.CheckBox("chkSelected", new { @class = "gridCheck", @value = c.ReportId })) 
      .Named("").Encode(false) 
      .Attributes(@class => "grid-column"); 


column.For(c => c.ReportName) 
       .Named("Buyer Close Sheet Name") 
       .SortColumnName("ReportName") 
       .Attributes(@class => "grid-column"); 

     }) 
     .Render();} 

<input type="submit" name=" DeleteReports" id=" DeleteReports" value="Delete" class="btnSubmit"/> 

</form> 
</html> 

Sau đó, trong điều khiển của bạn thực hiện phương pháp hành động của bạn

public ActionResult SubmitReportList (string ReportListSubmit, IList<string> 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"); 
    } 
Các vấn đề liên quan