ASP.NET MVC - 如何获得职位上的复选框值 [英] ASP.NET MVC - How to get checkbox values on post
问题描述
嘿,这是一个新手ASP.NET MVC的问题。
Hey all, this is a newbie ASP.NET MVC question.
我有了一个复选框列表和一个提交按钮的视图。在提交帖子到一个控制器方法,但我无法弄清楚如何获得的复选框的值。另外,我无法弄清楚如何让我传递到视图时,我在后的方法是模型数据,我尝试使用Html.Hidden但是这似乎并没有工作。
I have a view that has a list of checkboxes and a submit button. On submit it posts to a controller method but I can't figure out how to get the values of the checkboxes. Also, I can't figure out how to get model data that I passed into the view when I'm in the post method, I tried using Html.Hidden but that didn't seem to work.
这里的code:
http://pastebin.com/m2efe8a94 (查看)
http://pastebin.com/m39ebc6b9 (控制器)
Here's the code: http://pastebin.com/m2efe8a94 (View) http://pastebin.com/m39ebc6b9 (Controller)
感谢您的任何输入端接收,
贾斯汀
Thanks for any input received, Justin
推荐答案
我注意到的第一件事是,你的隐藏字段必须表单中。目前,在你看来,他们是BeginForm以上,所以他们不会被包含在表单提交。
First thing I noticed is that your hidden fields need to be inside your form. Currently in your view, they are above the BeginForm, so they won't be included in the form submission.
要得到的选中的复选框的值,一个IsOffered参数添加到您的OfferTrade操作方法。
To get the values of the selected check boxes, add an IsOffered parameter to your OfferTrade Action method.
public ActionResult OfferTrade(FormCollection result, List<string> IsOffered)
这参数将包含所有选中的IsOffered盒ITEMID的列表。
That parameter will contain a list of the ItemId's for all the checked IsOffered boxes.
本的HtmlHelper的复选框的工作方式不同,我不喜欢这样的工作方式,所以我不使用它。
The HtmlHelper's CheckBox works differently and I don't like the way it works, so I don't use it.
制作 IsOffered
参数类型列表&LT; INT方式&gt;
如果您ITEMID场是一个整数,也应该工作
Making the IsOffered
parameter type List<int>
should also work if your ItemId field is an integer.
这篇关于ASP.NET MVC - 如何获得职位上的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!