-2

I have problem with my code. I select a specific value using SelectList function but it seems it is not working. Here is my code:

Controller:

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

View:

<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>

And it seems it renders like this:

<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>

And since I set the SelectedValue from SelectList method it should suppose to be render the item something like this:

<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>

How come that It didn't select a specific value? Can someone help me with this?

Tseng
  • 61,549
  • 15
  • 193
  • 205
Alvin Quezon
  • 1,089
  • 3
  • 11
  • 29
  • You were told yesterday already, that you were using tags for old verison of ASP.NET MVC. [tag:asp.net] and [tag:asp.net-mvc] are for the **OLD ASP.NET MVC** webstack. Please use [tag:asp.net-core] and/or [tag:asp.net-core-mvc] when you have a question regarding ASP.NET Core – Tseng May 04 '17 at 11:14

1 Answers1

1

Tag Helper Confusion

It's important to note that the asp-for tag helper is going to function very similar to some of the older MVC HTML Helpers, in that, it will handle binding a specific property within the View. As such, you'll need to ensure that it points to whatever property represents the "Frequency Id" that is present on your Loan instances.

The asp-items tag helper in this instance won't really need a value, as that will be set by the asp-for helper on its own. You'll just need to build a list of your frequencies and their corresponding identifiers, and then pass in a collection of Loan objects to your View and let the tag helpers take care of the rest.

Example

First, let's define some classes for example purposes, which may be trivialized versions of your current ones:

// 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;
    }
}

You don't need to set a specific default value for your frequencies if you are going to use it simply to set using a property from another object (i.e. a Loan). So you can define your SelectList within your Controller along with some values that will be passed to your View:

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);
}

Within your View, you would just need to ensure that your asp-for property points to the frequency-related property of your Loan object, and that the asp-items attribute points to your collection of frequencies:

@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>

This should output something that looks like the following:

enter image description here

Notice that each of the <select> elements properly bound its selection to the corresponding value for the Frequency attribute from each of the loans passed into the View.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • Sorry I'm a bit new to programming with asp.net would you mind give me a sample code? I am still learning asp.net core. – Alvin Quezon May 04 '17 at 02:27
  • 1
    I've added an example, hope it helps. – Rion Williams May 04 '17 at 03:00
  • That will not bind when you submit the form (you cannot use a `foreach` loop - it generates `name` attributes that have no relation ship to the model - refer [Post an HTML Table to ADO.NET DataTable](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) –  May 04 '17 at 03:46