asp.net-mvc
  • checkbox
  • 2013-01-17 99 views 2 likes 
    2

    我有每个图像都有一个复选框旁边一些像这样的图片:通复选框控制器asp.net的MVC

    <input type="checkbox" name="select" value="<%=item.Id%>" /> 
    

    现在我要发送的选择复选框通过单击超链接到控制器。我有:

    <a href='<%: Url.Action("DeleteSelected", "Product", new { @ShopID = ViewBag.shopIDempty }) %>'>Delete</a> 
    

    和控制器:

    public ActionResult DeleteSelected(int[] select, int ShopID) 
        { 
    
         foreach (int checkBoxSelected in select) 
         { 
          //Do something...    
         } 
         return RedirectToAction("Index"); 
        } 
    

    但没有通到int []选择,它是空始终。哪里不对?

    +0

    如果您想将参数传递给DeleteSelected链接,您必须手动添加选中的复选框值以在其他情况下链接参数,您应该使用带按钮单击的表单和POST检查项复选框 – vadim

    回答

    0

    做这些==> 1)使其中含有所选择的复选框值

    var delete= new Array(); 
    
        $('.checkboxed').live('click', function() { 
           if ($(this)[0].checked == true) { 
            var itemdel= $(this).val(); 
            delete.push(itemdel); 
           } else { 
            var remve = $(this).val(); 
            for (var i = 0; i < delete.length; i++) { 
             if (delete[i] == remve) { 
              delete.splice(i, 1); 
              break; 
             } 
            } 
    
           } 
          }); 
    

    2)使上点击超链接

    $.ajax({ 
           type: 'POST', 
           contentType: 'application/json; charset=utf-8', 
           url: '/Product/DeleteSelected' 
    + '?ShopID =' + ShopIdValue, 
           dataType: 'json', 
           data: $.toJSON(delete), 
    
    
           success: function (result) { 
            window.location.href=result; 
           }, 
    
    
           async: false, 
           cache: false 
          }); 
    

    3的AJAX调用数组)让您的动作像这样

    public ActionResult DeleteSelected(int[] select) 
    { 
    var shopid= Request["ShopID "]; 
    } 
    
    0

    试试这个:

    [HttpPost] 
    public ActionResult DeleteSelected(FormCollection collection) 
    { 
        try 
        { 
         string[] test = collection.GetValues("select"); 
        } 
        catch (Exception ex) 
        { 
         return null; 
        } 
    } 
    

    我确实想要指出,您正在采取的方法需要一个表单来包装所有的复选框,或者您需要专门构建一个对象,以便在Syed显示时发送给控制器。如果您使用表单方式,则需要使用链接触发表单提交或将链接转换为提交按钮,并为商店ID指定一个隐藏字段。

    相关问题