如何使DropDownListFor不使用静态列表重复数据库数据? [英] How to make the DropDownListFor Don't repeat the Database data with the static list?

查看:91
本文介绍了如何使DropDownListFor不使用静态列表重复数据库数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DropDownListFor静态数字列表,如果我选择第一个月1并进行编辑将找到DropDownListFor像这样重复第一个月

I have a DropDownListFor a static list of numbers of months if i select the first month 1 and make edit will find the DropDownListFor repeat the first month like this

DropDownListFor代码:

DropDownListFor Code :

                    @Html.DropDownListFor(model=>model.FinancialInfo.FinancialExpiryMonth, (IEnumerable<SelectListItem>)ViewBag.FinancialMonths, (Model.FinancialInfo == null || Model.FinancialInfo.ExpiryDate == null) ? String.Empty : Model.FinancialInfo.ExpiryDate.Value.Month.ToString(), new { @class = "description-text" })

注意:FinancialInfo.FinancialExpiryMonth是一个元数据元素,使其成为View模型.

Note : the FinancialInfo.FinancialExpiryMonth is a Metadata element to make it as a View model.

ViewBag.FinancialMonths的代码:

Code of the ViewBag.FinancialMonths:

        ViewBag.FinancialMonths = ListOfNumbers(1, 12).Select(m => new SelectListItem { Text = m.ToString(), Value = m.ToString() });`

ListOfNumbers的代码:

Code of the ListOfNumbers:

    public List<int> ListOfNumbers(int startNum, int endNum)
    {
        List<int> listOfNumbers = new List<int>();
        for (int i = startNum; i <= endNum; i++)
        {
            listOfNumbers.Add(i);
        }
        return listOfNumbers;
    }

推荐答案

您看到的第一个"1"是占位符,它是由

The first "1" that you see is a placeholder and it is generated by

(Model.FinancialInfo == null || Model.FinancialInfo.ExpiryDate == null) ? String.Empty : Model.FinancialInfo.ExpiryDate.Value.Month.ToString()

将下拉列表定义替换为:

Replace the dropdown definition with:

@Html.DropDownListFor(model=>model.FinancialInfo.FinancialExpiryMonth, (IEnumerable<SelectListItem>)ViewBag.FinancialMonths, "Please select...", new { @class = "description-text" }) 

您将看到'Please select ...'作为占位符

And you will see 'Please select...' as a placeholder

为了便于预先选择一个月,只需为model.FinancialInfo.FinancialExpiryMonth

In order so pre-select a month simply assign a value to model.FinancialInfo.FinancialExpiryMonth

这篇关于如何使DropDownListFor不使用静态列表重复数据库数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆