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();
                }

            } 

  • 相关阅读:
    openCV学习——一、图像读取、显示、输出
    caffe配置文件
    5.卷积神经网络
    【caffe】基本数据结构blob
    URLSearchParams对象
    window.history对象
    在React项目中添加ESLint
    memorization-根据输入重新计算render的数据
    React重置非受控组件state的方法
    彻底搞清楚DOM元素的height,offsetHeight,clientHeight,scrollHeight
  • 原文地址:https://www.cnblogs.com/chshnan/p/2107551.html
Copyright © 2011-2022 走看看