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

            } 

  • 相关阅读:
    ubuntu下内核源码树的建立
    删除ubuntu旧版本内核
    设置ubuntu12.04桌面版开机进入命令行模式
    MFC学习笔记(一)向模态对话框传递数据
    redis 映射数据结构粗略
    redis入门
    mybatis总结
    mybatis--mapper配置总结
    mybatis-初步使用
    maven-plugins说明
  • 原文地址:https://www.cnblogs.com/chshnan/p/2107551.html
Copyright © 2011-2022 走看看