c# - 如何在 ASP.NET MVC 中设置来自模型的下拉列表的值

标签 c# asp.net asp.net-mvc razor asp.net-mvc-5

我卡在“如何将下拉列表中的值设置为模型中设置的值”。

我正在添加到目前为止的内容和尝试过的内容。

所以,我有一个 PaymentFormMV 模型,它还有另一个模型,它是 PaymentCCMV

public class PaymentFormMV
{
    public PaymentCCMV CreditDetails { get; set; }
    public string ModelPropertyPrefixName
    {
        get
        {
            return this.ModelPropertyPrefix.Replace("_", ".");
        }
    }
}
public class PaymentCCMV
{
    [Display(Name = "Expiration Date")]
    public int ExpirationMonth { get; set; }
}

cshtml 文件从 PaymentFormMV 获取数据

<div class="form-group">
    @Html.LabelFor(model => model.CreditDetails.ExpirationMonth, htmlAttributes: new { @class = "control-label col-md-2 required" })
    <div class="col-md-10">
        @Html.DropDownList(Model.ModelPropertyPrefixName + "CreditDetails.ExpirationMonth", Helpers.PossibleMonths,
        new { id = Model.ModelPropertyPrefix + "CreditDetails_ExpirationMonth", @class = "form-control", style = "width:70px;" })
    </div>
</div>

Helpers.PossibleMonths:显示所有月份

public static ICollection<SelectListItem> PossibleMonths
{
    get
    {
        return new List<SelectListItem>()
        {
            new SelectListItem() { Text = "1 - Jan", Value = "1" },
            new SelectListItem() { Text = "2 - Feb", Value = "2" },
            new SelectListItem() { Text = "3 - Mar", Value = "3" },
            new SelectListItem() { Text = "4 - Apr", Value = "4" },
            new SelectListItem() { Text = "5 - May", Value = "5" },
            new SelectListItem() { Text = "6 - Jun", Value = "6" },
            new SelectListItem() { Text = "7 - Jul", Value = "7" },
            new SelectListItem() { Text = "8 - Aug", Value = "8" },
            new SelectListItem() { Text = "9 - Sep", Value = "9" },
            new SelectListItem() { Text = "10 - Oct", Value = "10" },
            new SelectListItem() { Text = "11 - Nov", Value = "11" },
            new SelectListItem() { Text = "12 - Dec", Value = "12" },
        };
    }
}

积分:

我得到整数值model.CreditDetails.ExpirationMonth = 2(根据数据库)

但我不知道如何将其设置为下拉列表。因此,当屏幕加载时,我希望将该值绑定(bind)到下拉列表。请指导我。所以,如果值为 2,我应该在下拉 UI 中得到类似 2 - Feb 的内容,如 Helper 方法中所述。我是初学者,所以我可能没有遵循最佳实践,所以请多多包涵。

生成的 HTML:

enter image description here

最佳答案

我支持@Vivien 的回复,但我建议使用采用选定值 ( See MSDN: ) 的重载,然后您可以设置该值。然后允许传递默认值,我还建议使用静态方法,并允许传递月份:

public static SelectList PossibleMonths(string defaultValue)
{
        return new SelectList(List<SelectListItem>()
        {
            new SelectListItem() { Text = "1 - Jan", Value = "1" },
            new SelectListItem() { Text = "2 - Feb", Value = "2" },
            new SelectListItem() { Text = "3 - Mar", Value = "3" },
            new SelectListItem() { Text = "4 - Apr", Value = "4" },
            new SelectListItem() { Text = "5 - May", Value = "5" },
            new SelectListItem() { Text = "6 - Jun", Value = "6" },
            new SelectListItem() { Text = "7 - Jul", Value = "7" },
            new SelectListItem() { Text = "8 - Aug", Value = "8" },
            new SelectListItem() { Text = "9 - Sep", Value = "9" },
            new SelectListItem() { Text = "10 - Oct", Value = "10" },
            new SelectListItem() { Text = "11 - Nov", Value = "11" },
            new SelectListItem() { Text = "12 - Dec", Value = "12" },
        }, "Value", "Text", defaultValue);
}

编辑:您的下拉菜单可能看起来像

@Html.DropDownList(Model.ModelPropertyPrefixName + "CreditDetails.ExpirationMonth", Helpers.PossibleMonths(Model.CreditDetails.ExpirationMonth),
                new { id = Model.ModelPropertyPrefix + "CreditDetails_ExpirationMonth", @class = "form-control", style = "width:70px;" })

假定 CreditDetails 对象始终不为空,否则必须进行空检查。

编辑:我错了,显然 Html.DropDownList 不支持 SelectList,所以我恢复到原来的方法,但对设置所选值做了一些改动(请注意上面相同的下拉代码才能正常工作)。

public static IEnumerable<SelectListItem> PossibleMonths(string defaultValue)
{
        if (defaultValue == null)
           defaultValue = "";

        return new List<SelectListItem>()
        {
            new SelectListItem() { Text = "1 - Jan", Value = "1", Selected = (defaultValue == "1") },
            new SelectListItem() { Text = "2 - Feb", Value = "2", Selected = (defaultValue == "2") },
            new SelectListItem() { Text = "3 - Mar", Value = "3", Selected = (defaultValue == "3") },
            new SelectListItem() { Text = "4 - Apr", Value = "4", Selected = (defaultValue == "4") },
            new SelectListItem() { Text = "5 - May", Value = "5", Selected = (defaultValue == "5") },
            new SelectListItem() { Text = "6 - Jun", Value = "6", Selected = (defaultValue == "6") },
            new SelectListItem() { Text = "7 - Jul", Value = "7", Selected = (defaultValue == "7") },
            new SelectListItem() { Text = "8 - Aug", Value = "8", Selected = (defaultValue == "8") },
            new SelectListItem() { Text = "9 - Sep", Value = "9", Selected = (defaultValue == "9") },
            new SelectListItem() { Text = "10 - Oct", Value = "10", Selected = (defaultValue == "10") },
            new SelectListItem() { Text = "11 - Nov", Value = "11", Selected = (defaultValue == "11") },
            new SelectListItem() { Text = "12 - Dec", Value = "12", Selected = (defaultValue == "12") },
        };
}

关于c# - 如何在 ASP.NET MVC 中设置来自模型的下拉列表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40593619/

相关文章:

C#/报表查看器 : How to use parameters without specifying parameter names

c# - Bot Framework 搞乱了对话框状态

javascript - 使用 javascript 检索 session (在列表中)

c# - 如何从 Web API 应用程序返回 PDF

asp.net-mvc - ClaimsIdentity 上的 BootstrapContext 为 null

javascript - MVC3 Razor - Jquery 的 Foreach 问题

c# - .Net 中的大容量、高速文本框

c# - 使用 InvokeMember 检索静态属性值

c# - 如何隐藏或显示 Asp :Content using a Session in If statement

c# - 存在显式转换(您是否缺少转换?)