Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hey all, this is a newbie ASP.NET MVC question.

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.

Here's the code: http://pastebin.com/m2efe8a94 (View) http://pastebin.com/m39ebc6b9 (Controller)

Thanks for any input received, Justin

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
155 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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)

That parameter will contain a list of the ItemId's for all the checked IsOffered boxes.

The HtmlHelper's CheckBox works differently and I don't like the way it works, so I don't use it.

Making the IsOffered parameter type List<int> should also work if your ItemId field is an integer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...