locked
how to bind to a select list RRS feed

  • Question

  • User1457412228 posted

    Hi,

    I have a for loop that loops through previous addresses and I am attempting to bind it, but am having no luck.

    How would I bind my Select to the value for MyAddresses[i].State .

    (I am doing the same thing with start date and end date.)

    <select asp-for="MyAddresses[i].State">
    <option value="AL">AL</option>
    <option value="AK">AK</option>
    <option value="AZ">AZ</option>
    <option value="AR">AR</option>
    <option value="CA">CA</option>
    <option value="CO">CO</option>
    <option value="CT">CT</option>
    <option value="DE">DE</option>

    etc, etc, etc.

    </select>

    Monday, July 1, 2019 10:02 PM

Answers

  • User-854763662 posted

    Hi talldaniel ,

    If you want to bind the selected value to MyAddresses[i].State ,  refer to the working example below :

    One-To -Many relationship between Person model and MyAddress model

      public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List<MyAddress> MyAddresses { get; set; }
        }
    
      public class MyAddress
        {
            public int Id { get; set; }
            public string Address { get; set; }
    
            public string State { get; set; }
        }

    View

    @model WebAppDemo.Models.People.Person
    <form asp-action="Edit"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <input type="hidden" asp-for="Id" /> <div class="form-group"> <label asp-for="Name" class="control-label"></label> <input asp-for="Name" class="form-control" /> <span asp-validation-for="Name" class="text-danger"></span> </div> @for (int i = 0; i < Model.MyAddresses.Count; i++) { <input type="hidden" asp-for="MyAddresses[i].Id" /> <div class="form-group"> <label asp-for="MyAddresses[i].Address" class="control-label"></label> <input asp-for="MyAddresses[i].Address" class="form-control" /> <span asp-validation-for="MyAddresses[i].Address" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="MyAddresses[i].State" class="control-label"></label> <select asp-for="MyAddresses[i].State" class="form-control"> <option value="AL">AL</option> <option value="AK">AK</option> <option value="AZ">AZ</option> <option value="AR">AR</option> <option value="CA">CA</option> <option value="CO">CO</option> <option value="CT">CT</option> <option value="DE">DE</option> </select> <span asp-validation-for="MyAddresses[i].Address" class="text-danger"></span> </div> } <div class="form-group"> <input type="submit" value="Save" class="btn btn-default" /> </div> </form>

    Controller

           [HttpPost]
           [ValidateAntiForgeryToken]
           public async Task<IActionResult> Edit(int id,Person person)
            {
                if (id != person.Id)
                {
                    return NotFound();
                }
    
                if (ModelState.IsValid)
                {
                    try
                    {
                        _context.Update(person);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!PersonExists(person.Id))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return RedirectToAction(nameof(Index));
                }
                return View(person);
            }

    About Select Tag Helper in ASP.NET Core MVC , you could refer to here .

    Best Regards , 

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 2, 2019 3:11 AM

All replies

  • User1457412228 posted

    I think I need to reinstall Visual Studio, as I did not change the code and it started working, and something similar happened on my previous question.

    Also, when I open VS 2019 Preview version, it seems to corrupt my code, but when I open it in VS2017 again ,then the code is not corrupted.

    So, anyway, please ignore the above question.  The code is fine, as is.  It did not work, and then it did.

    Tuesday, July 2, 2019 3:11 AM
  • User-854763662 posted

    Hi talldaniel ,

    If you want to bind the selected value to MyAddresses[i].State ,  refer to the working example below :

    One-To -Many relationship between Person model and MyAddress model

      public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List<MyAddress> MyAddresses { get; set; }
        }
    
      public class MyAddress
        {
            public int Id { get; set; }
            public string Address { get; set; }
    
            public string State { get; set; }
        }

    View

    @model WebAppDemo.Models.People.Person
    <form asp-action="Edit"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <input type="hidden" asp-for="Id" /> <div class="form-group"> <label asp-for="Name" class="control-label"></label> <input asp-for="Name" class="form-control" /> <span asp-validation-for="Name" class="text-danger"></span> </div> @for (int i = 0; i < Model.MyAddresses.Count; i++) { <input type="hidden" asp-for="MyAddresses[i].Id" /> <div class="form-group"> <label asp-for="MyAddresses[i].Address" class="control-label"></label> <input asp-for="MyAddresses[i].Address" class="form-control" /> <span asp-validation-for="MyAddresses[i].Address" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="MyAddresses[i].State" class="control-label"></label> <select asp-for="MyAddresses[i].State" class="form-control"> <option value="AL">AL</option> <option value="AK">AK</option> <option value="AZ">AZ</option> <option value="AR">AR</option> <option value="CA">CA</option> <option value="CO">CO</option> <option value="CT">CT</option> <option value="DE">DE</option> </select> <span asp-validation-for="MyAddresses[i].Address" class="text-danger"></span> </div> } <div class="form-group"> <input type="submit" value="Save" class="btn btn-default" /> </div> </form>

    Controller

           [HttpPost]
           [ValidateAntiForgeryToken]
           public async Task<IActionResult> Edit(int id,Person person)
            {
                if (id != person.Id)
                {
                    return NotFound();
                }
    
                if (ModelState.IsValid)
                {
                    try
                    {
                        _context.Update(person);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!PersonExists(person.Id))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return RedirectToAction(nameof(Index));
                }
                return View(person);
            }

    About Select Tag Helper in ASP.NET Core MVC , you could refer to here .

    Best Regards , 

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 2, 2019 3:11 AM
  • User1457412228 posted

    Thank you.

    It appears my Visual Studio installation is corrupt, which is very rare, but does happen.  

    Tuesday, July 2, 2019 3:13 AM