skip to Main Content

I have a page where I am passing a viewModel containing a list. I’m trying to fill the form with the values from the viewModel list. It works, except for the select element.

The select element keeps defaulting to the Viewbag.rateList that I’m pushing there.

Is there a way to populate the select element?

View:

@for (var i = 0; i < 6; i++)
{
    var rateRequest = Model.RateRequests[i];
    <div class="rate-request-row">
        <!-- Input fields for rate request properties -->
        <!-- Use Model.RateRequests[i].Property to bind to the RateRequest properties -->
        <div class="col-md-12" style="display:inline-flex;">
            <input class="form-control col-md-2" type="date" name="RateRequests[@i].ServiceDate" value="@rateRequest.ServiceDate.ToString("yyyy-MM-dd")" />
            <input class="form-control col-md-2" type="time" name="RateRequests[@i].ServiceTime" value="@rateRequest.ServiceTime.ToString("hh\:mm")" />
            <select class="form-control col-md-3" name="RateRequests[@i].RateCode">
                <option value="@rateRequest.RateCode[i]"></option>
                @foreach (var item in ViewBag.rateList)
                {
                    <option value="@item.Value">@item.Text</option>
                }
            </select>
            <input class="form-control col-md-1" type="number" name="RateRequests[@i].Quantity" placeholder="Quantity" value="@rateRequest.Quantity" />
            <input class="form-control col-md-1" type="number" name="RateRequests[@i].Modifier" step=".01" placeholder="Modifier" value="@rateRequest.Modifier" />
        </div>
    </div>
}

Controller [in case you need it]

[HttpPost]
    public IActionResult TwoRateBuilder(RateQuoteVM rateQuoteVM, string zero)
    {
        ViewBag.zero = zero;
        // Populate the dropdown list for rate codes
        List<SelectListItem> rateList = new List<SelectListItem>();
        var allRates = _context.ListRates.Select(rate => new { rate.RateCode, rate.RateName }).ToList();
        rateList.Add(new SelectListItem() { Value = "", Text = "" });
        foreach (var rate in allRates)
        {

            rateList.Add(new SelectListItem() { Value = rate.RateCode, Text = rate.RateName });
        }
        ViewBag.rateList = rateList;

        // Remove rate requests with empty rate codes
        rateQuoteVM.RateRequests = rateQuoteVM.RateRequests
            .Where(rr => !string.IsNullOrWhiteSpace(rr.RateCode))
            .ToList();


        List<RateQuoteFactory.RateQuoteIndex> allQuotes = new List<RateQuoteFactory.RateQuoteIndex>();

        foreach (var rateRequest in rateQuoteVM.RateRequests)
        {


            var quotes = _rateQuoteFactory.RateQuote(rateRequest.ServiceDate, rateRequest.ServiceTime, null, rateRequest.RateCode, rateRequest.Quantity, rateRequest.Modifier, null);

            
            allQuotes.AddRange(quotes);
        }

        property
        rateQuoteVM.Quotes = allQuotes;

        // Ensure there are exactly 6 rate requests in the view model
        while (rateQuoteVM.RateRequests.Count < 6)
        {
            rateQuoteVM.RateRequests.Add(new RateRequest
            {
                ServiceDate = TimeZoneFactory.GetTimeEST().AddDays(1),
                ServiceTime = TimeSpan.Parse("8:00"),
                RateCode = "",
                Quantity = 1,
                Modifier = 1.0m
            });
        }


        //return RedirectToAction("TwoRateBuilder", "Post", new { zero });
        return View(rateQuoteVM);
    }

2

Answers


  1. something like

     <select id="yourID" name="yourname" asp-items="@ViewBag.rateList">
    

    or

      <select class="form-select-sm" asp-for="Model.RateRequests"
     asp-items="@ViewBag.rateList"></select>
    

    isn’t work for you ?

    Login or Signup to reply.
  2. For your title:

    Razor Select Element is not showing vm value
    because you didn’t assign the text value,

    <option value="@rateRequest.RateCode[i]"> you need assign text value</option>
    

    Juding from

    Is there a way to populate the select element?

    The controller you’ve shown is for post request

    and this line in your view:

    <option value="@rateRequest.RateCode[i]"></option>
    

    I think you are asking :

    Is there a way to populate the selected element?

    You could compare the value of the option with RateCode ,if they are equal(which means you selected it and send the post request),mark it as selected:

    @foreach (var item in (List<SelectListItem>)ViewBag.rateList)
        {
            if (item.Value == @rateRequest.RateCode[i])
            {
                <option value="@item.Value" selected>@item.Text</option>
            }
            else
            {
                <option value="@item.Value">@item.Text</option>
            }
           
        }
    

    Remove <option value="@rateRequest.RateCode[i]"></option> to avoid duplicated option

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search