你究竟如何从MVC2复选框中获得返回值

时间:2011-08-30 19:33:47

标签: asp.net-mvc-2 checkbox html-helper

我的某个表单上有一个复选框。不幸的是,我似乎无法弄清楚如何从服务器端(我的控制器)获取价值。在山姆山你怎么做?看来你必须检查表格元素何时不是假的? 你是怎么做到的?

添加一些附加信息         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");
                    return RedirectToAction("Home", "Home");
                }
            }

这是我的观点(我有一个复选框)

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

更新2

检查我的返回值,返回“true,false” 这有什么意义呢?

1 个答案:

答案 0 :(得分:0)

如果你没有使用强类型视图模型,你可能会做这样的事情:

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

在这种情况下,您可以在将其发送到服务器时访问它,如下所示:

 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. 
 }

如果您使用的是强类型视图模型,则可以采用相同的方式:

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

然后访问:

 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. 
 }