asp.net-mvc-3 – 如何将单选按钮与ASP.Net MVC中的模型数据进行绑定?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 如何将单选按钮与ASP.Net MVC中的模型数据进行绑定?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想以我的形式显示一个将从模型数据填充的单选按钮.

这是我的模特儿

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

在这里,我正在尝试从动作方法手动填充学生模型

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

现在我怎么可以生成一个单选按钮,它将以Male&女性和值将具有ID

搜索了谷歌,发现很多样本,我使用了一个但不确定它是否可行.
这里是单选按钮代码.

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

我不想硬编码男性或女性;我想通过一个模型显示它,并希望在for循环中生成单选按钮.

我也是新MVC,所以请指导我.

解决方法

如果我理解正确,您应该更改您的学生模型,以使属性“性别”为一个整数,然后您应该有另一个属性称为“SexList”,您填充列表.此更改将允许您发布数据并检索用户选择的性别.

如果你使用Razor视图引擎,你应该这样做:

@{ 
    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>
    }
}

编辑:

你的模型应该是这样的:

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 Sex Gender { get; set; }

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

}

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

您在控制器中的操作:

[HttpGet]
public AcitonResult Index()
{
    var student = new Student 
    {
        FirstName = "Rion",//I think the best way to populate this list is to call a service here.
        SexList = new List<Sex>
        {
            new Sex{ID="1",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.Gender,new { id = "sex" + sex.ID })
                @Html.Label("sex" + sex.ID,sex.Type)
            </div>
        }
    }

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

你应该在你的控制器中采取这样的行动,这是提交将发布数据的地方:

[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相关文章