SelectList不选择特定值

时间:2017-05-04 02:15:29

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

我的代码有问题。我使用SelectList函数选择一个特定的值,但它似乎无效。这是我的代码:

控制器:

Public IActionResult Index()
{
   myList.loanFrequency = new SelectList(lstLoanFrequency, "Id", "Value", 4);
}

查看:

<div class="form-group">
        <label>Loan Frequency</label>
        <select class="form-control" asp-for="loanContract.LoanFrequencyId" asp-items="@Model.loanFrequency" disabled></select>
        <small class="form-text text-muted">Upon registration to this loan you are set to a <b>Biweekly</b> because your payroll mostly computed and deducted every Biweekly day of the month.</small>
    </div>

它看起来像是这样:

<select class="form-control" disabled="" data-val="true" data-val-required="The LoanFrequencyId field is required." id="loanContract_LoanFrequencyId" name="loanContract.LoanFrequencyId">
<option value="1">Weekly</option>
<option value="2">Biweekly</option>
<option value="3">Semimonthly</option>
<option value="4">Monthly</option>//Not rendered in HTML
<option value="5">Bimonthly</option>
<option value="6">Quarterly</option>
<option value="7">Semiannually</option>
<option value="8">Annually</option>
</select>

因为我从SelectList方法设置SelectedValue,所以它应该假设为这样的项目呈现:

<select class="form-control" disabled="" data-val="true" data-val-required="The LoanFrequencyId field is required." id="loanContract_LoanFrequencyId" name="loanContract.LoanFrequencyId">
<option value="1">Weekly</option>
<option value="2">Biweekly</option>
<option value="3">Semimonthly</option>
<option value="4" selected>Monthly</option>//This should be rendered in the HTML
<option value="5">Bimonthly</option>
<option value="6">Quarterly</option>
<option value="7">Semiannually</option>
<option value="8">Annually</option>
</select>

为什么它没有选择特定值?有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

标记助手混乱

值得注意的是,asp-for标记帮助程序的功能与一些较旧的MVC HTML帮助程序非常相似,因为它将处理绑定View中的特定属性。因此,您需要确保它指向代表贷款实例中存在的“频率ID”的任何属性。

此实例中的asp-items标记帮助程序实际上不需要值,因为它将由asp-for帮助程序自行设置。您只需要构建一个频率列表及其相应的标识符,然后将一组Loan对象传递给您的View,让标签助手处理剩下的事情。

示例

首先,让我们定义一些类作为示例目的,这可能是您当前版本的简单版本:

// This represents the frequency a loan would occur
public class LoanFrequency
{
    public int Id { get; set; }
    public string Value { get; set; }

    public LoanFrequency(int id, string value)
    {
        Id = id;
        Value = value;
    }
}

// This is a very basic example of a loan
public class Loan
{
    public string Name { get; set; }
    public int Frequency { get; set; }

    public Loan(string name, int frequency)
    {
        Name = name;
        Frequency = frequency;
    }
}

如果您打算使用其他对象(即贷款)中的属性进行设置,则无需为频率设置特定的默认值。因此,您可以在Controller中定义SelectList以及将传递给您的视图的一些值:

public IActionResult Index()
{
        // Example loan frequencies
        ViewBag.LoanFrequencies = new SelectList(new LoanFrequency[]
        {
            new LoanFrequency(1, "Weekly"),
            new LoanFrequency(2, "Biweekly"),
            new LoanFrequency(3, "Semimonthly"),
            new LoanFrequency(4, "Monthly"),
            new LoanFrequency(5, "Bimonthly"),
            new LoanFrequency(6, "Quaterly"),
            new LoanFrequency(7, "Semiannually"),
            new LoanFrequency(8, "Annually")
        }, "Id", "Value");

        // Example loans to bind
        var loans = new Loan[]
        {
            new Loan("Weekly Loan", 1),
            new Loan("Monthly Loan", 4),
            new Loan("Birthday Loan", 8)
        };

        return View(loans);
}

在您的视图中,您只需要确保asp-for属性指向Loan对象的频率相关属性,asp-items属性指向您的收藏集频率:

@model YourExampleApplication.Models.Loan[]

<ul>
    @foreach (var loan in Model)
    {
        <li>
            @loan.Name
            <select asp-for="@loan.Frequency" asp-items="ViewBag.LoanFrequencies"></select>
        </li>
    }
</ul>

这应该输出如下所示的内容:

enter image description here

请注意,每个<select>元素都将其选择正确地绑定到传递给View的每个贷款的Frequency属性的相应值。