2011-08-30 56 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. 
}