Answer the question
In order to leave comments, you need to log in
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
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>
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 questionAsk a Question
731 491 924 answers to any question