【问题标题】:In Update view, the dropdown selected value is not set if the selected field is nullable in viewmodel在更新视图中,如果所选字段在视图模型中可为空,则未设置下拉选择值
【发布时间】:2014-05-01 20:12:15
【问题描述】:

比如说我的viewmodel如下:

public class MyViewModel
{
    public int? SelectedCategoryId { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set; } 
}

而视图如下:

 @model MyViewModel

    @Html.DropDownListFor(
        x => x.SelectedCategoryId,
        Model.Categories,
        "--Select--"
    )

在更新视图中,如果模型具有 SelectedCategoryId 的值,则它不会在下拉列表中显示为选中状态。始终选择“--Select--”。如果它们的 Id 字段不可为空,则相同的逻辑适用于其他下拉列表。

有什么建议吗?

【问题讨论】:

  • 所以即使 SelectedCategoryID 中有一个值,它也只是显示“--Select--”?
  • 是的,即使 SelectedCategoryID 有一个值,它也会显示“--Select--”。

标签: asp.net-mvc-4 razor html.dropdownlistfor


【解决方案1】:

向您推荐一个简单的方法。
如果您使用IEnumerable &lt;SelectListItem&gt;,则无需使用不同的变量来维护选定值。只需在填充Categories SelectList 时标记您的类别。

SelectListItem category = new SelectListItem
            {
                Text = "CategoryId",
                Value = "CatagoryName",
                Selected = true
            };

Categories.Add(category);

public class MyViewModel
{
    public IEnumerable<SelectListItem> Categories { get; set; } 
}  

..................................

@model 我的视图模型

@Html.DropDownList("Name",
    Model.Categories,
    "--Select--"
)

【讨论】:

  • 我无法更改模型。我必须使用可空字段来保存所选字段。感谢您的建议。
【解决方案2】:

最后,如果 Id 有值,我使用 jquery 选择下拉菜单。

在视图中,我创建了一个隐藏字段(不要使用@Html.HiddenFor)

 @Html.Hidden("SelectedCategorySubId", Model.SelectedCategorySubId.GetValueOrDefault(), new { @id = "hdnSelectedCategorySubId" })

在JS文件中,在ajax调用的回调方法中(加载下拉),将数据添加到下拉后,获取隐藏字段值,然后在下拉中选择该值。

 $.ajax({
            type: 'POST',
            url: "GetSubCategory",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(param),
            success: function (subCategoryList) {

                if (subCategoryList != null && subCategoryList != '') {

                    $("#SelectedCategorySubId").append('<option>--- Select ---</option>');
                    $.each(subCategoryList, function (i, subCategory) {

                        $("#SelectedCategorySubId").append($('<option/>', {
                            value: subCategory.Value,
                            text: subCategory.Text
                        }));
                    });

                 //Set selected value from hidden field
                    var selectedCategorySubId = $("#hdnSelectedCategorySubId").val();
                  // alert("Sub Cat" + selectedCategorySubId);
                    $("#SelectedCategorySubId").val(selectedCategorySubId);

                }//end if
                else {
                    //alert("empty subcategoryList " + subCategoryList);
                    $("#tdSubCategory").hide();

                }
            }

如果下拉列表发生变化,则更新隐藏字段

//Update hidden field, if cat changes
$("#SelectedCategorySubId").change(function () {
    $("#hdnSelectedCategorySubId").val($(this).val());
});

我希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    相关资源
    最近更新 更多