Reputation: 39
I use mvc html helper checkbox like this:
@Html.DropDownList(item.QuestionId.ToString(),
new SelectList(((DropDownList)item).Options, "OptionId", "Name"),
"Select Please")
and when i submit, in my [http] controller
, i can get the value by
[HttpPost]
public ActionResult TestList(FormCollection formCollection)
{
foreach (var res in formCollection.AllKeys)
{
string Selected = null;
Selected = formCollection[res];
}
but when I select "A" for example, I get A, but when I select nothing (it shows "Select Please" as I defined), I want it to be null
,but now it's ""
, it means it has value...I just want it to be like: I submit nothing if I selected nothing, I mean nothing in AllKeys, neither in StringSelected
, I want allkeys has nothing if I don't select, thank you
ps: for e.g:
foreach (var SubItem in ((RadioButtonList)item).Options)
{
@Html.RadioButton(item.SurveyItemId.ToString(), SubItem.OptionId) @SubItem.Name
}
it will show options of a question, if i chose A, i can get A form my controller, i didnt choose B,so in my controller AllKeys, it doesnt contain B or C or D, so i want dropdownlist just like this, if i didnt choose anyone, it wont show "" or something else just like i didnt choose B or C or D, nothing will show in AllKeys
Upvotes: 1
Views: 516
Reputation: 93484
First, that's not a check box. That's a drop down list. Asking questions with correct information is important, or you will get the wrong answers.
Second, this is not possible, and it has nothing to do with MVC, this is done by the browser. There's no way to make the browser submit "null" when you're actually submitting something. There is no concept of "null" in an http post.
The only way you get null is if the value is not posted at all. Otherwise, the model binder could not know the difference between an actual null and when you actually wanted to post an empty string.
You can't even create a custom model binder, because you're bypassing model binding by going directly to the forms collection.
Upvotes: 3