获取控制器中复选框的选中状态

时间:2012-04-19 17:17:46

标签: c# asp.net-mvc-3

net mvc3应用程序我有一个复选框:

<input type="checkbox" id="daStores" name="CheckBox1" onclick="filter()" />

如果是否从控制器检查了它,我该怎么办?

类似这样的事情

public ActionResult GoToPage(string page)
{
    bool ischecked = //get the checked status from the view
}

1 个答案:

答案 0 :(得分:3)

这是我要展示的极其简单(且不切实际)的代码,但您必须将表单提交给控制器。

HTML:

<form action="MyController/MyAction" method="POST">
   <input type="checkbox" id="daStores" name="CheckBox1" onclick="filter()" />
   <input type="submit" value="Submit" />
</form>

控制器:

[HttpPost]
public ActionResult MyAction(bool Checkbox1)
{
    bool ischecked = Checkbox1;
}

听起来你对ASP.NET MVC的工作原理知之甚少,我认为你应该先尝试使用一些入门教程,然后才能直接进入。

http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3