D
D
Dmitry2019-03-14 16:01:52
HTML
Dmitry, 2019-03-14 16:01:52

How to pass the value value from the view to the controller when selecting the select value?

The cshtml view has a drop down list. How, when selecting one value from the list, pass the value to the controller, which in turn will reload this view based on the value received?

<select required>
         @for (int i = 0; i < @ViewBag.screenList.Count; i++) {
                    <option value="@i">@ViewBag.screenList[i]</option>
          }
</select>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-03-18
@Alarih1

Found a solution:

<form asp-controller="Patients" asp-action="Details">
           <select required onchange="this.form.submit()" name="screenData">
                       @for (int i = 0; i < @ViewBag.screenList.Count; i++) {
                                    if (@ViewBag.screenList[i] == ViewBag.screenData) {
                                            <option value="@ViewBag.screenList[i]" selected>@ViewBag.screenList[i]</option>
                                    } else { <option value="@ViewBag.screenList[i]">@ViewBag.screenList[i]</option> }
                        }
           </select>
</form>

while name="screenData" in the select must match the parameter in the controller method

D
Denis Bogdanov, 2019-03-15
@den-bogdanov

You most likely need to use a listener on the change event on the select element. In the event handler, receive value
Something like this:

const select = document.getElementById("select");

select.addEventListener("change", function(e) {
  console.log("value", e.target.value);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question