Home > database >  Retrieve object from select drop down list
Retrieve object from select drop down list

Time:10-28

I am trying to return a meetingSpace object from the last form group in this razor page:

<form method="post">
    <input type="hidden" asp-for="Booking.Id" />
    <div class="form-group">
        <label asp-for="Booking.Name"></label>
        <input asp-for="Booking.Name" class="form-control" />
        <span class="text-danger" asp-validation-for="Booking.Name"></span>
    </div>

    <div class="form-group">
        <label asp-for="Booking.Start">Start</label>
        <input asp-for="Booking.Start" class="form-control" />
        <span class="text-danger" asp-validation-for="Booking.Start"></span>
    </div>
    <div class="form-group">
        <label asp-for="Booking.Duration">Duration</label>
        <input asp-for="Booking.Duration" class="form-control" />
        <span class="text-danger" asp-validation-for="Booking.Duration"></span>
    </div>

    <div class="form-group">
        <label asp-for="Booking.MeetingSpace">Room</label>
        <select data-value="true" data-va-required="The meeting space field is required" id="MeetingSpace.Id" name="MeetingSpace.Name">
            @foreach (var meetingSpace in Model.meetingSpaces)
            {
                <option value="meetingSpace">@meetingSpace.Name</option>
            }
        </select>
    </div>


    <button type="submit" class="btn btn-primary">Save</button>
</form>

I am unsure how set this select dropdown to return the value to the Booking.MeetingSpace property. I can see the list of meetingSpace objects in the drop down but after selecting an option the meetingSpace field in the created/edited object is not returned as with the other properties in the form. I have also tried the same using the Booking.MeetingSpaceId int as well but it is still not populated after submission of the form.

CodePudding user response:

it could be a binding problem try to change the name attribute value like this:

<select data-value="true" data-va-required="The meeting space field is required" id="Booking.MeetingSpace.Id" name="Booking.MeetingSpace.Name">
            @foreach (var meetingSpace in Model.meetingSpaces)
            {
                <option value="@meetingSpace.Id">@meetingSpace.Name</option>
            }
</select>

also option value should start with @ to be recognized.

  • Related