@html剃刀单选按钮mvc5 [英] @html razor radio buttons mvc5

查看:443
本文介绍了@html剃刀单选按钮mvc5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的视图模型

public class UserResponseModel
    {
        public string QuestionId { get; set;} 

        public string QuestionText { get; set; }

        public bool IsChecked { get; set; }


    }

所以,对于这个复选框精美作品

so, for checkboxes this works beautifully

for (var i = 0; i < Model.UserResponses.Count; i++)
    {
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">

                    @Html.HiddenFor(x => x.UserResponses[i].QuestionId)
                    @Html.CheckBoxFor(x => x.UserResponses[i].IsChecked) 

但单选按钮,这并不

but for radio buttons this does not

 for (var i = 0; i < Model.UserResponses.Count; i++)
    {
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">

                    @Html.HiddenFor(x => x.UserResponses[i].QuestionId)
                    @Html.RadioButtonFor(x => x.UserResponses[i].IsChecked, new { Name = "grp1" })
                    @Html.DisplayTextFor(x => x.UserResponses[i].QuestionText)

当我提交表单,然后是器isChecked总是假的 - 为什么 - 我失去了什么 - 因为我的复选框提到它工作得很好。我没有看这个问题,<一个href=\"http://stackoverflow.com/questions/17987360/asp-net-mvc-razor-how-to-show-grouped-radio-buttons-for-two-model-fields\">here但我不知道为什么单选按钮需要在视图模型额外的属性保持正确的答案时复选框透明地工作。

when i submit the form then IsChecked is always false - why - what am i missing - as I mentioned for checkboxes it works just fine. I did look at this question here but i'm not sure why radiobuttons require extra properties in the view model to hold the correct answer when checkboxes work transparently.

编辑:现在我的问题模型是如此

my question model is now as so

public class QuestionModel
    {
        public string WhichQuestion { get; set; }

        public int PointsObtained { get; set; }

        public bool CorrectAnswer { get; set; }

        private List<UserResponseModel> _userResponse;
        public List<UserResponseModel> UserResponses 
        {
            get { return _userResponse ?? (_userResponse = new List<UserResponseModel>()); }
            set { _userResponse = value; }
        }
    }

我刚才说公布尔CorrectAnswer通知{搞定;组; }

notice i have just added public bool CorrectAnswer { get; set; }

在我看来,这里的code

and in my view here's the code

for (var i = 0; i < Model.UserResponses.Count; i++)
    {
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">

                    @Html.HiddenFor(x => x.UserResponses[i].QuestionId)
                    @Html.HiddenFor(x => x.CorrectAnswer)
                    @Html.HiddenFor(x => x.UserResponses[i].IsChecked)
                    @Html.RadioButtonFor(x => x.UserResponses[i].IsChecked, Model.CorrectAnswer, new { Name = "grp1" })

编辑2:

 @Html.HiddenFor(x => x.UserResponses[i].QuestionId)
                    @Html.HiddenFor(x => x.SelectedAnswerId)
                    @Html.HiddenFor(x => x.UserResponses[i].IsChecked)
                    @Html.RadioButtonFor(x => x.UserResponses[i].IsChecked,    Model.SelectedAnswerId, new { Name = "grp1" })

EDIT3:

public class QuestionModel
    {
        public string WhichQuestion { get; set; }

        public int PointsObtained { get; set; }



        private List<UserResponseModel> _userResponse;
        public List<UserResponseModel> UserResponses 
        {
            get { return _userResponse ?? (_userResponse = new List<UserResponseModel>()); }
            set { _userResponse = value; }
        }
    }

public class UserResponseModel
    {
        public string QuestionId { get; set;} 

        public string QuestionText { get; set; }



        public string SelectedQuestionId { get; set; }

    }

终于,我的看法

for (var i = 0; i < Model.UserResponses.Count; i++)
    {
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">

                    @Html.HiddenFor(x => x.UserResponses[i].QuestionId)
                    @Html.RadioButtonFor(x => x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId, new { Name = "grp1" })
                    @*@Html.CheckBoxFor(x => x.UserResponses[i].IsChecked)*@
                    @Html.DisplayTextFor(x => x.UserResponses[i].QuestionText)

EDIT4:所以最后我得到一些地方
这个作品!

so finally I'm getting some where this works!!!

@Html.RadioButtonFor(x => x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId)

我现在可以看到selectedquestionid填充在我httppost方法,但如果我这样做

I can now see the selectedquestionid populated in my httppost method but if I do this

@Html.RadioButtonFor(x => x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId, new {Name="grp"}) 

然后我虽然能只有一个选择单选按钮的selectedquestionid的的空对httppost - 奇怪

then though i am able to select only one radiobutton the selectedquestionid is null on httppost - wierd

推荐答案

单选按钮不复选框。单选按钮进行分组。这意味着你必须在自己的视图模型的单一属性,以保持选定的单选按钮值(如图所示 回答您链接到 )。随着复选框,你可以选择了多个复选框,这是你的你是他们绑定到布尔值的集合在你的视图模型。另一方面单选按钮只有一个可能的选择的值,因为它们是相互排斥的。这就是为什么他们都应该被绑定在您的视图模型中的一个属性,将举行选定的单选按钮的值。

Radio buttons are not checkboxes. Radio buttons are grouped. This means that you should have a single property on your view model to hold the selected radio button value (as shown in the answer you linked to). With check boxes you could have multiple checkboxes selected, that's thy you are binding them to a collection of boolean values in your view model. Radio buttons on the other hand have only one possible selected value because they are mutually exclusive. That's why they should all be bound to a single property on your view model that will hold the selected radio button value.

因此​​,在你的问题和答案的例子,你必须将包含问题编号和文字,以及可能的答案列表的问题视图模型。每个答案将是一个id和文本psented重新$ P $。你的问题视图模型也将有一个答案的属性,将举行从用户选择的答案单选按钮。正是这种特性,你会到所有的单选按钮绑定。但不同的值。

So in your example of questions and answers, you have a question view model that will contain the question id and text as well as a list of possible answers. Each answer will be represented by an id and text. Your question view model will also have an answer property that will hold the selected answer radio button from the user. It is this property that you are going to bind all your radio button to. But with a different value.

这篇关于@html剃刀单选按钮mvc5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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