2011-08-30 55 views
0

我在其中一个表单上有一个复选框。不幸的是,我似乎无法弄清楚如何从服务器端(我的控制器)获得价值。你在萨姆山怎么做?看起来你必须检查表格的时间是否为假?
你是如何做到这一点的?如何从MVC2复选框获得返回值

添加一些附加信息 如果((的FormCollection [ “popID”]!= NULL)& &(的FormCollection [ “开始日期”]!= NULL)& &(的FormCollection [ “结束日期”]!= NULL) & &(formCollection [“sendAnyway”]!= null)) { string popId = formCollection [“popID”];如果(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

检查它返回我的返回值, “真,假” 什么样的这样做的感觉是什么?

+0

我们可能需要看到您的控制器的行动,你正在模型?或表单域作为参数? – Patricia

+0

请发表您的代码示例,以便我们可以为您的问题提供一些解决方案......由于可能存在多个场景,因此很难提供帮助 – cpoDesign

回答

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