如何从视图中选择发送复选框列表来控制 [英] How to send a list of selected checkboxes from view to controller

查看:102
本文介绍了如何从视图中选择发送复选框列表来控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出我怎样才能选择复选框列表中使用ActionLink的工作。我想我需要做一些客户方使用JavaScript,但无法找到相关的code。

I have been trying to work out how I can get the list of selected checkboxes to work using an ActionLink. I think I need to do something clientside with JavaScript but cannot find the relevant code.

以下code完美的作品使用一个提交按钮,回发所选择的ID作为ID的数组,但我需要有这样与其他按钮的页面上。

The following code works perfectly using a submit button, posting back the selected id's as an array of id's, but I need to have this on a page with other buttons.

// the view 
@foreach (var station in Stations)
{
   <input type="checkbox" name="selected" value="@station.StationId" /> 
}    
<input type="submit" value="Save" />

//Controller stub
public ActionResult Action(string [] selected) 

我一直停留在这几个小时,所以也许我在看这种错误的方式。

I have been stuck on this for hours, so maybe I am looking at this the wrong way.

PS。我的很多很多时间阅读和学习在这里后的第一篇文章。

PS. My first post after many many hours reading and learning here.

推荐答案

看起来你是不是在找AJAX职位。解决这个最简单的方法就是把它包起来的形式和调用提交功能。这里是你的code应该是什么样子:

Looks like you are not looking for AJAX post. The easiest way to tackle this is to wrap it in the form and call submit function. Here is what your code should look like:

@using(Html.BeginForm("uraction", "urcontroller", FormMethod.Post, new { id = "formId" })) { 
    foreach(var station in Stations) {
        <input type="checkbox" name="selected" value="@station.StationId" /> 
    }
}
<a href="#" id="postBtn">Post</a>
<script>
    $(document).ready(function() {
        $('#postBtn').click(function() {
            $('#formId').submit();
        }
    }
</script>

这篇关于如何从视图中选择发送复选框列表来控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆