开发者

How on earth do you get the return value from an MVC2 checkbox

I have a checkbox on one of my forms. And unfortunately, I cant't seem to figure out how to get the value out of the thing on the server side (my controlller). How in sam hill do you do that? It seems like you have to do a check for when the form ellement if not false?

How do you do this?

Adding some addition information if ((formCollection["popID"] != null) && (formCollection["StartDate"] != null) && (formCollection["EndDate"] != null) && (formCollection["sendAnyway"] != null)) { string popId = formCollection["popID"]; if (formCollection["StartDate"] != "") { startDate = DateTime.Parse(formCollection["StartDate"]); }

                if (formCollection["EndDate"] != "")
                {
                    endDate = DateTime.Parse(formCollection["EndDate"]);
                }

                Boolean sendAnyway = Boolean.Parse(formCollection["sendAnyway"]);

                if (client.ProcessGetABBYPopulation(popId, startDate, endDate, sendAnyway) == false)
                {
                    return PutToQError("Error placing message on Queue");
                }
                else
                {

                    //return Success("You successfully placed a message on the Queue");
                    retur开发者_如何学JAVAn RedirectToAction("Home", "Home");
                }
            }

Here is my view (where I have a checkbox)

     <%:Html.Label("Reprocess patients already sent during this timeframe?") %>
     <%:Html.CheckBox("sendAnyway") %>

Update 2

Checking my return value it returns, "true,false" what kind of sense does that make?


If you're not using a strongly typed view model, you're probably doing something like this:

 <% Html.CheckBox("someName") %>

In which case, you can access it when you POST it to your server like so:

 public ActionResult SomeMethod(string someName) // Bound via name on the parameter
 {
     string value = Request.Form["someName"]; // Or pulled from the Form collection
     // someValue / value = "1" or whatever your checkbox value was, IF SELECTED. 
     // If unselected, it's NULL. 
 }

If you're using a strongly typed view model, it's available the same way:

 <% Html.CheckBoxFor(x => x.IsSet) %>

And then accessed like:

 public ActionResult SomeMethod(MySampleViewModel vm)
 {
      vm.IsSet; // true if selected, false if not
      string request = Request.Form["IsSet"];
      // request = value of your checkbox, NULL otherwise. 
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜