到底如何从 MVC2 复选框获取返回值

发布于 2024-12-02 04:44:25 字数 1534 浏览 2 评论 0原文

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

添加一些附加信息 if ((formCollection["popID"] != null) && (formCollection["StartDate"] != null) && (formCollection["EndDate"] != null) && (formCollection[ “发送无论如何”]!= null)) { 字符串 popId = formCollection["popID"]; if (formCollection["开始日期"] != "") { 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” 这有什么意义呢?

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");
                    return 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素手挽清风 2024-12-09 04:44:25

如果您不使用强类型视图模型,则可能会执行以下操作:

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

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

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

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. 
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文