【问题标题】:In MVC Razor Why are my checkboxes and dropdown not picking up the selected value or checked value on submit在 MVC Razor 中,为什么我的复选框和下拉菜单在提交时没有选择选定的值或选中的值
【发布时间】:2021-02-26 01:09:40
【问题描述】:

我正在使用 MVC Razor 视图。

在我的表单顶部,我有以下内容:

@using (Html.BeginForm())

我的复选框、单选按钮或下拉菜单似乎都不起作用,它们都返回 null 值

checkbox : model.Domestic 是一个布尔值,这可能是个问题吗?返回空

  @Html.CheckBoxFor(model => model.Domestic.Value, new { value = Model.Domestic.Value ? "checked" : "" })&nbsp;&nbsp;<span class="display-checkbox">Domestic</span>

单选按钮返回 null

@Html.RadioButtonFor(model => model.Subscription_Renewal, false, new { id = "SR_ChkNo", value = "checked", onclick = "checkButtonDt();" })&nbsp;&nbsp;&nbsp;<span class="largeText_light">N/A</span>

下拉:

后端代码:

 // Create the Categories Select List 
                var categoryList = new List<SelectListItem>();
                foreach (Category c in returned.Categories)
                {
                    categoryList.Add(new SelectListItem
                    {
                        Value = c.Category_ID.ToString(),
                        Text = c.Description,
                        Selected = (c.Category_ID == returned.Category_ID ? true : false)

                    });
                }

                returned.CategoryList = categoryList;

然后在视图中:我试图从所选列表中选择一个条目,它返回 0

@Html.DropDownList("category", Model.CategoryList, new { @class = "input-box" })

【问题讨论】:

    标签: c# razor asp.net-core-mvc


    【解决方案1】:

    这是一个在发布表单时将数据与复选框、单选按钮和下拉列表绑定的演示:

    型号:

    public class BindModel 
        {
            public List<SelectListItem> CategoryList { get; set; }
            public string Subscription_Renewal { get; set; }
            public bool Domestic { get; set; }
            public string category { get; set; }
    
    
        }
    

    查看:

    @using (Html.BeginForm())
    {
        @Html.CheckBoxFor(model => model.Domestic)<span class="display-checkbox">Domestic</span>
        <input type="radio" name="Subscription_Renewal" value="Male" /><span class="largeText_light">Male</span>
        <input type="radio" name="Subscription_Renewal" value="Female" /><span class="largeText_light">Female</span>
        <input type="radio" name="Subscription_Renewal" value="Unspecified" /><span class="largeText_light">Unspecified</span>
        @Html.DropDownList("category", Model.CategoryList, new { @class = "input-box" })
        <input type="submit" value="submit" />
    }
    

    控制器:

    public IActionResult TestBind(BindModel bindModel) {
                BindModel returned = new BindModel();
                var categoryList = new List<SelectListItem> { 
                    new SelectListItem {  Value="1", Text="Category1",Selected=true}, 
                    new SelectListItem { Value = "2", Text = "Category2", Selected = false }, 
                    new SelectListItem { Value = "3", Text = "Category3", Selected = false } };
                
    
                returned.CategoryList = categoryList;
                return View(returned);
            }
    

    结果:

    【讨论】:

    • 在这里感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多