【问题标题】:Radio button values binding not working单选按钮值绑定不起作用
【发布时间】:2018-02-12 16:11:52
【问题描述】:

我有一个看法:

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

我正在努力完成下一步:

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

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

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

还有一件事,我尝试创建局部视图,但现在仍然很幸运。我在网上阅读到将 for/foreach 循环放在视图中是一种不好的做法,并试图简化它,但这种方法也不起作用。

谢谢

【问题讨论】:

  • 我不明白您为什么在 Post 操作中期望 2 个属性。您是否尝试过接收您的模型类?您在 Get 操作中发送的同一类。

标签: c# html asp.net-mvc


【解决方案1】:

我注意到一些问题 -

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

结果

发布价值

【讨论】:

  • re dot point 3 - id 属性应该被删除(使用new { id = "" })或使其唯一。您的代码正在生成重复的 id 属性。
  • 这有帮助。谢谢
  • 为什么 DepartmentCode 为空?如何将它们设置为不为空?
  • @MilošĐošović,如果你想绑定它,你需要为它创建一个输入
  • @StephenMuecke 所以它不会返回已经传递给视图的值?
猜你喜欢
  • 2020-08-04
  • 2015-07-30
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
  • 2015-10-25
  • 1970-01-01
相关资源
最近更新 更多