如何绑定单选按钮与模型数据ASP.Net MVC [英] How to bind radio button with model data ASP.Net MVC

查看:593
本文介绍了如何绑定单选按钮与模型数据ASP.Net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想表明我的形式将模型数据填充单选按钮。

i want to show radio button in my form which will be populated from model data.

public class Student
    {
        [Required(ErrorMessage = "First Name Required")] // textboxes will show
        [Display(Name = "First Name :")]
        [StringLength(5, ErrorMessage = "First Name cannot be longer than 5 characters.")]
        public string FirstName { get; set; }

[Required(ErrorMessage = "Last Name Required")] // textboxes will show
        [Display(Name = "Last Name :")]
        [StringLength(5, ErrorMessage = "Last Name cannot be longer than 5 characters.")]
        public string LastName { get; set; }

[Required(ErrorMessage = "Sex Required")] // group of radio button will show
        [Display(Name = "Sex :")]
        public List<Sex> Sex { get; set; }

}

public class Sex
    {
        public string ID { get; set; }
        public string Type { get; set; }
    }

在这里,我想从操作方法手动填充学生模型像

here i am trying to populate student model manually from action method like

public AcitonResult Index()
{
var student=  new Student 
{
    FirstName = "Rion",
    LastName = "Gomes",

    Sex= new List<Sex>
        {
            new Sex{ID="1" , Type = "Male"}, 
            new Sex{ID="2" , Type = "Female"}
        }    
}    
    return View(student);
}

现在我怎么能产生无线电按钮,将在形式上与男性放显示文本;女和值将有 ID

now how could i generate radio button which will display text in form Male & Female and as value will have the ID

我搜索谷歌和发现了很多样本​​,我用一个,但没有确定它的工作原理。
这里是鉴于单选按钮code。

i search google and found many sample and i used one but no sure does it work. here is the radio button code in view.

@Html.RadioButtonFor(x => x.Sex, "Male")

但我不想硬code男性或女性,而我想通过展示其模型,并希望在循环中产生的单选按钮。

but i do not want to hard code male or female rather i want to show it through model and want to generate the radio button in for loop.

我在MVC新。所以请指导我。

i am new in MVC. so please guide me.

推荐答案

如果我理解正确,你应该改变你的学生模型,以拥有的财产性作为一个整数,然后你应该叫SexList其他属性在您填充列表。这一变化将允许你发布你的数据和检索由用户选择性别。

If I understood correctly you should change your Student model in order to have the property "Sex" as an integer and then you should have another property called "SexList" where you populate the list. This change will allow you to post your data and retrieve the sex selected by the user.

如果您正在使用的Razor视图引擎,你应该做这样的事情:

If you are using Razor view engine you should do something like this:

@{ 
    foreach (var sex in Model.SexList)
    {
        <div>
            @Html.RadioButtonFor(model => model.Sex, new { id = "sex" + sex.ID })
            @Html.Label("sex" + sex.ID, sex.Type)
        </div>
    }
}

编辑:

您的模型应该是这样的:

Your model should be something like this:

    public class Student
    {
        [Required(ErrorMessage = "First Name Required")] // textboxes will show
        [Display(Name = "First Name :")]
        [StringLength(5, ErrorMessage = "First Name cannot be longer than 5 characters.")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Last Name Required")] // textboxes will show
        [Display(Name = "Last Name :")]
        [StringLength(5, ErrorMessage = "Last Name cannot be longer than 5 characters.")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Sex Required")]
        [Display(Name = "Sex :")]
        public int Sex { get; set; }

        public List<Sex> SexList { get; set; }

}

您的控制器动作:

[HttpGet]
public AcitonResult Index()
{
    var student = new Student 
    {
        FirstName = "Rion",
        LastName = "Gomes",

        //I think the best way to populate this list is to call a service here.
        SexList = new List<Sex>
        {
            new Sex{ID="1" , Type = "Male"}, 
            new Sex{ID="2" , Type = "Female"}
        }    
    }    

    return View(student);
}

和查看:

@Html.BeginForm()
{
    <div>
        @Html.LabelFor(model => model.FirstName)
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div>
        @Html.LabelFor(model => model.LastName)
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
    @{ 
        foreach (var sex in Model.SexList)
        {
            <div>
                @Html.RadioButtonFor(model => model.Sex, new { id = "sex" + sex.ID })
                @Html.Label("sex" + sex.ID, sex.Type)
            </div>
        }
    }

    <input type="submit" value"Submit" />
}

和你应该在你的控制器的动作这种方式,这就是提交将要发布的数据的地方:

and you should have an action in your controller this way, this is the place where the submit is going to post the data:

[HttpPost]
public AcitonResult Index(Student model)
{
    if (ModelState.IsValid)
    {
        //TODO: Save your model and redirect 
    }

    //Call the same service to initialize your model again (cause we didn't post the list of sexs)
    return View(model);
}

这篇关于如何绑定单选按钮与模型数据ASP.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆