单选按钮值绑定不起作用

时间:2018-02-12 16:11:53

标签: c# html asp.net-mvc

我有一个观点:

    @using(Html.BeginForm())
{
    <div>
        <table id="dimensionFeedbackTable">
            <tbody>
                @for(int i = 0; i < 6; i++)
                {
                    <tr>
                        <td>
                            <div>
                                <p>
                                    <text>
                                        Rate @i
                                    </text>
                                </p>
                            </div>
                        </td>
                        @foreach(var item in Model.DeptList)
                        {
                            <td>
                                <div style="text-align: center;">
                                    @Html.RadioButtonFor(m => item.DepartmentValue, i,
                                            new
                                            {
                                                id = i,
                                                style = "min-height:45px;"
                                            })
                                </div>
                            </td>
                        }
                    </tr>
                }

            </tbody>
        </table>


        <button id="submitComment" value="submit">
            @{
                <text>Submit</text>
            }
        </button>
    </div>
}

该视图中的模型是EmployeeModelClass。这是:

public class EmployeeModelClass
    {
        public int Id
        {
            get; set;
        }

        public IEnumerable<DepartmentModelClass> DeptList
        {
            get; set;
        }
    }

    public class DepartmentModelClass
    {
        public string DepartmentCode
        {
            get; set;
        }

        [Range(1, 6)]
        public int? DepartmentValue
        {
            get; set;
        }
    }

我的控制器:

[HttpPost]
        public ActionResult Index(EmployeeModelClass emp, IEnumerable<DepartmentModelClass> qec)
        {
            emp.DeptList = qec;
            return View(emp);
        }

我正在努力完成下一步: enter image description here

每一行都应该是自我组合。例如,Rate 1行只能选择一个单选按钮。现在,应该绑定这些按钮。如果在任何行中选择了第二个单选按钮,则其值(DepartmentValue)应设置为2.因此,当我提交时,我会收集选择哪个单选按钮的集合。

我尝试了很多组合设置正确的模型并绑定它。还尝试了html辅助方法RadioButton,但仍然没有运气返回值。我设法得到的最接近的是返回一个集合,但是虽然在Controller中选择了单选按钮,但集合不为null,但值(DepartmentValues)为空。

此外,这里有类似的答案,但由于某些原因,它们都不适合我。任何帮助和指点方向表示赞赏。对一件简单的事情失去了理智。

还有一件事,我尝试创建局部视图,但现在仍然运气好。我在网上看到,在视图中放置/ foreach循环是一种不好的做法,并试图简化它,但这种方法也无效。

由于

1 个答案:

答案 0 :(得分:2)

我注意到几个问题 -

  • for循环和foreach循环相反。
  • 应该使用for代替foreach
  • 来控制数组
  • 不要将ID明确指定给单选按钮new { id = i, ...})

视图

@model DemoMvc.Models.EmployeeViewModel
@using (Html.BeginForm())
{
    <div>
        <table id="dimensionFeedbackTable">
            <tbody>
                @for (int i = 0; i <  Model.DepartmentViewModels.Count; i++){
                    <tr>
                        <td>
                            <div>
                                <p>
                                    <text>
                                        Rate @Model.DepartmentViewModels[i].DepartmentCode
                                    </text>
                                </p>
                            </div>
                        </td>
                        @for (int j = 1; j <= 6; j++)
                        {
                            <td>
                                <div style="text-align: center;">
                                    @Html.RadioButtonFor(m => 
                                        Model.DepartmentViewModels[i].DepartmentValue, 
                                        j, new { style = "min-height:45px;" }) 
                                    @j
                                </div>
                            </td>
                        }
                    </tr>
                }
            </tbody>
        </table>
        <button id="submitComment" value="submit">Submit</button>
    </div>
}

模型

public class EmployeeViewModel
{
    public int Id { get; set; }
    public IList<DepartmentViewModel> DepartmentViewModels { get; set; }
}

public class DepartmentViewModel
{
    public string DepartmentCode { get; set; }
    public int? DepartmentValue { get; set; }
}

控制器

public ActionResult Index()
{
    // This could come from database.
    var model = new EmployeeViewModel
    {
        DepartmentViewModels = new List<DepartmentViewModel>
        {
            new DepartmentViewModel{ DepartmentCode = "ABC", DepartmentValue = 1},
            new DepartmentViewModel{ DepartmentCode = "DEF", DepartmentValue = 2},
            new DepartmentViewModel{ DepartmentCode = "GHI", DepartmentValue = 3},
        }
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(EmployeeViewModel model)
{
    return View(model);
}

结果

enter image description here

发布值

enter image description here