• Asp.Net mvc CheckBox,RadioButton使用方法


    在实体User中建立selectlist的属性

    public SelectList CheckBoxList { get; set; }
    public SelectList RadioButtonList { get; set; }

    新建一个A实体做为键值操作

    public class A
        {
            public int value{get;set;}
            public string text{get;set;}
        }

     调用数据

    protected SelectList CheckBoxList(object defaultvalue)
            {
                List<A> obja = new List<A>()
                {
                    new A(){text="第一",value=1},
                    new A(){text="第二",value=2},
                    new A(){text="第三",value=3},
                    new A(){text="第四",value=4},
                };
                return new SelectList(obja, "value", "text", defaultvalue);
            }
            protected SelectList RadioButtonList(object defaultvalue)
            {
                List<A> obja = new List<A>()
                {
                    new A(){text="第一",value=1},
                    new A(){text="第二",value=2},
                    new A(){text="第三",value=3},
                    new A(){text="第四",value=4},
                };
                return new SelectList(obja, "value", "text", defaultvalue);
            }

     action中初始化

    public ActionResult Index()
            {
                
                User objuser = new User()
                {
                    Email = "objectboy@msn.com",
                    Name = "objectboy",
                    CheckBoxList = CheckBoxList(3), //调用
                    RadioButtonList=RadioButtonList(2) //调用
                };
                return View(objuser);
            }

     页面调用

    @model MvcApplication2.Models.User
    
    .............
    
    
    
      <div class="editor-field">
                @Html.DropDownList("SLlist1", Model.CheckBoxList,"请选择")
            </div>
            <div class="editor-field">
                @foreach (SelectListItem item in Model.CheckBoxList)
                {
                    @Html.CheckBox("SomeParas", item.Selected, new { value = item.Value })
                    @Html.Label(item.Text);
                }
            </div>
            <div class="editor-field">
                @foreach (SelectListItem item in Model.RadioButtonList)
                {
                    @Html.RadioButton("dd", item.Value, item.Selected);
                    @Html.Label(item.Text);
    
                }

    注意:控件的名字不能与实体中属性名相同,要不无法初始化
  • 相关阅读:
    a-b(高精度)
    a+b(高精度)[陈涛]——c语言
    |-病毒集锦-|(!长期更新!)
    2016/7/7 二分警察(搜查)~#递归打法好!
    2016/7/7 自定义函数copy
    2015/7/6 (!长期更新!)C语言从零——张呵呵
    2016/7/6 高精准计算器-减(神·高精准)
    2016/7/6 神·CPU的人类极限在哪?
    2016/7/6 真·高精度计算器-加 (火速跟完!!!)
    2016/7/6 准·高精度计算器-加
  • 原文地址:https://www.cnblogs.com/jake-ge/p/6676692.html
Copyright © 2020-2023  润新知