locked
Display name instead of id in ASP.Net MVC using scaffolding RRS feed

  • Question

  • User1768077170 posted

    Maybe this is very simple to do , but I can't find the answer by scaffolding when I search  on google.

    I have 2 models Organizer and Conference. As you can see, they have one to many relationship. I used the scaffolding for both of them. Everything works fine but when i use the "create.cshtml" i have a dropdown list of OrganizerId. Instead of that, i want a dropdown list of Name in the Organizer table. How can i do that ? Thank you.

    Conference.cs and Organizer.cs and Create.cshtml

    public class Conference
        {
            public int ConferenceId { get; set; }
            public string Title { get; set; }
            public DateTime Date { get; set; }
            public string Location { get; set; }
            public int NumerOfSessions { get; set; }
            public int RegistrationFee { get; set; }
            
            public int OrganizerId { get; set; }
            public virtual Organizer Organizer { get; set; }
    }
    public class Organizer
        {
            public int OrganizerId { get; set; }
            public string Name { get; set; }
            public string Email { get; set; }
            public uint Phone { get; set; }
    
            public virtual ICollection<Conference> Conferences { get; set; }
        }
    @model APTA.Models.Conference
    
    @{
        ViewData["Title"] = "Create";
    }
    
    <h1>Create</h1>
    
    <h4>Conference</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Create">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Title" class="control-label"></label>
                    <input asp-for="Title" class="form-control" />
                    <span asp-validation-for="Title" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Date" class="control-label"></label>
                    <input asp-for="Date" class="form-control" />
                    <span asp-validation-for="Date" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Location" class="control-label"></label>
                    <input asp-for="Location" class="form-control" />
                    <span asp-validation-for="Location" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="NumerOfSessions" class="control-label"></label>
                    <input asp-for="NumerOfSessions" class="form-control" />
                    <span asp-validation-for="NumerOfSessions" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="RegistrationFee" class="control-label"></label>
                    <input asp-for="RegistrationFee" class="form-control" />
                    <span asp-validation-for="RegistrationFee" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="OrganizerId" class="control-label"></label>
                    <select asp-for="OrganizerId" class ="form-control" asp-items="ViewBag.OrganizerId"></select>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
    

    Here is the page

    https://imgur.com/a/LzjVsJN

    Monday, April 13, 2020 1:53 PM

Answers

  • User475983607 posted

    DangNguyen

    i have tried doing it like this

    <div class="form-group">
                    <label asp-for="Organizer.Name" class="control-label"></label>
                    <select asp-for="Organizer.Name" class ="form-control" asp-items="ViewBag.Organizer.Name"></select>
                </div>

    but it did not work. i tried putting different variables in this area but so far either i get an error or the dropdown list does not have any data in it.

    I started to think the problem is in my ConferenceController.cs file

    public IActionResult Create()
            {
                ViewData["ConferenceId"= new SelectList(_context.Organizer, "ConferenceId""ConferenceId");
                return View();
            }

    Please review your code for correctness.  The View does not match the controller.

    The construct in the Action is...

    ViewBag.Options = new SelectList(_context.Organizer, nameof(Organizer.OrganizerId), nameof(Organizer.Name));

    The View.

    <select asp-for="Organizer.OrganizerId" class ="form-control" asp-items="@(SelectList)ViewBag.Options"></select>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2020 5:08 PM

All replies

  • User475983607 posted

    See the following blog.

    https://www.learnrazorpages.com/razor-pages/forms/select-lists

    Monday, April 13, 2020 2:25 PM
  • User1768077170 posted

    Thank you for your reply. I have read the blog and still can not come up with a solution for my problem.

    Monday, April 13, 2020 3:09 PM
  • User475983607 posted

    Thank you for your reply. I have read the blog and still can not come up with a solution for my problem.

    So far you have not showed any code that populate the select.  Can you share your updated code you tried after reading the blog?  

    Monday, April 13, 2020 4:16 PM
  • User611346789 posted

    Please look into

    ViewBag.OrganizerId

    from your controller
    Monday, April 13, 2020 4:20 PM
  • User1768077170 posted

    i have tried doing it like this

    <div class="form-group">
                    <label asp-for="Organizer.Name" class="control-label"></label>
                    <select asp-for="Organizer.Name" class ="form-control" asp-items="ViewBag.Organizer.Name"></select>
                </div>

    but it did not work. i tried putting different variables in this area but so far either i get an error or the dropdown list does not have any data in it.

    I started to think the problem is in my ConferenceController.cs file

    public IActionResult Create()
            {
                ViewData["ConferenceId"= new SelectList(_context.Organizer, "ConferenceId""ConferenceId");
                return View();
            }
    Monday, April 13, 2020 4:34 PM
  • User475983607 posted

    DangNguyen

    i have tried doing it like this

    <div class="form-group">
                    <label asp-for="Organizer.Name" class="control-label"></label>
                    <select asp-for="Organizer.Name" class ="form-control" asp-items="ViewBag.Organizer.Name"></select>
                </div>

    but it did not work. i tried putting different variables in this area but so far either i get an error or the dropdown list does not have any data in it.

    I started to think the problem is in my ConferenceController.cs file

    public IActionResult Create()
            {
                ViewData["ConferenceId"= new SelectList(_context.Organizer, "ConferenceId""ConferenceId");
                return View();
            }

    Please review your code for correctness.  The View does not match the controller.

    The construct in the Action is...

    ViewBag.Options = new SelectList(_context.Organizer, nameof(Organizer.OrganizerId), nameof(Organizer.Name));

    The View.

    <select asp-for="Organizer.OrganizerId" class ="form-control" asp-items="@(SelectList)ViewBag.Options"></select>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2020 5:08 PM
  • User1768077170 posted

    Thank you so much. I understand how it works now.

    Monday, April 13, 2020 5:21 PM