zoukankan      html  css  js  c++  java
  • MVC中checkbox的使用

    @Html.CheckBox和@Html.CheckBoxFor这两个方法一直没有用好,为了尽快多个CheckBox并且可以回传数据,使用了下面的办法,怎么看怎么觉得笨,不过先这样吧:

    在View页中:

    <div class="dropdownlist-field">
                @if (Model != null)
                {
                    foreach (var item in Model.lstUserRole)
                    {
                        if (item.bCheck)
                        {
                            <input type="checkbox" id="checkboxRole" name="checkboxRole" value="@item.role.RoleName" checked=true /><span> @item.role.RoleName</span>
                        }
                        else
                        {
                           <input type="checkbox" id="checkboxRole" name="checkboxRole" value="@item.role.RoleName" /><span> @item.role.RoleName</span>
                        }         
                    }
                }

            </div> 

    注意:id和name都一样。

     在Control中:

    [HttpPost]
            public ActionResult Edit(UserWrapper userWp, FormCollection collection)
            {
                try
                {
                    if (userWp.user != null)
                    {
                        UpdateModel(userWp.user);
                        //
                        if(collection.GetValues("checkboxRole")!=null)//这是判断name为
    checkboxRole的checkbox的值是否为空,若为空返回NULL;
                        {
                            string strRoles = collection.GetValue("checkboxRole").AttemptedValue;//
    AttemptedValue返回一个以,分割的字符串
                            string[] lstRoles = strRoles.Split(',');
                            foreach (string r in lstRoles)
                            {
                                ......
                            }
                        }
                        sysMg.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        //throw new NoSuchRecordException
                        return View();
                    }
                }
                catch
                {
                    return View();
                }

            } 

  • 相关阅读:
    2017 湖南省赛 K Football Training Camp
    一些相似单词的区别之处
    LeetCode301. Remove Invalid Parentheses
    算法刷题细节点总结
    LeetCode765. Couples Holding Hands
    LeetCode741. Cherry Pickup
    LeetCode312. Burst Balloons
    LeetCode679. 24 Game
    LeetCode862. Shortest Subarray with Sum at Least K
    LeetCode818. Race Car
  • 原文地址:https://www.cnblogs.com/chshnan/p/2107551.html
Copyright © 2011-2022 走看看