zoukankan      html  css  js  c++  java
  • 转摘:Aspnet mvc Html.Checkbox 处理

    This post will explain how to get the value of a checkbox when a form is posted within the world of ASP.NET MVC. This post will explain this approach by first presenting a common problem and then showing you the recommended solution. You can just jump to the solution by clicking here.

    The Common Problem

    A lot of times, you will ask a user to “sign up” or “register” to access your web application. Sometimes, this process will involve asking a user to agree to the terms of  the site. This task usually relies on the user checking a checkbox stating that they agree to be a nice user. For instance, you can see an example of one such registration page here.

    After a user has agreed, and decided to proceed, you should make sure they checked the checkbox on the server side.  The reason for this is to prevent malicious developers from attacking your site. Regardless, if you are using ASP.NET MVC, you may be surprised to learn that getting the value of a checkbox is a little different from what you may be expecting. For instance, imagine you have the following HTML page defined:

    <html xmlns=”http://www.w3.org/1999/xhtml” >
      <head><title>Check Test</title></head>
      <body><form action=”/” method=”post”>
        <%= Html.CheckBox(”chkHuman”, false)%> Are you human?<br />
        <input type=”submit” value=”Move Along” />
      </form></body>
    </html>

    Now imagine that when the user clicks the “Move Along” button, the following action is triggered:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection formValues)
    {
      string value = formValues["chkHuman"];
      return View();
    }

    If the user checks the checkbox, and this action gets executed, you will notice the “value” variable will be set to “true,false”. If the checkbox was not selected, the “value” variable will simply be “false”.  While you could just check the length, or manually parse the value, there is a more elegant solution.

    The Solution

    The recommended approach would be to write your code as follows:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection formValues)
    {
      bool isChecked = false;
      if (Boolean.TryParse(Request.Form.GetValues(”chkHuman”)[0], out isChecked) == false)
        ModelState.AddModelError(”chkHuman”, “Nice try.”);
      return RedirectToAction(”Index”);
    }

    In this approach, we are relying on the Request.Form object. By indexing the first element of the value returned by the GetValues method, we will get either “true” or “false”. This can then be easily converted to a bool. The fact that the value can easily be converted to a bool is what makes it the recommended approach.

    原文

  • 相关阅读:
    751时尚公开课(三)宋杰林:时尚大跃进_豆瓣
    第九课堂-经验与技能分享交易网站
    751D·PARK北京时尚设计广场_百度百科
    2014马上有乐趣 每周六免费服装缝纫体验课_豆瓣
    设计工作室寻求合作
    【基础穿搭法】关于穿衣显高和好看的三个小技巧。(请深爱)
    给快播指一条生路:转型会员付费吧
    美华美依 | 创业谱
    服装配饰_MAVIN MARVY 高级服装定制_西服定制_衬衫定制_西装定制
    探索者系列_百度百科
  • 原文地址:https://www.cnblogs.com/philzhou/p/1938524.html
Copyright © 2011-2022 走看看