Select-Tag Helper .NET Core to Controller

2019-08-27 16:24发布

问题:

I have tried to submit tag-select with 'submit' button and it worked properly but how about without submit button ?

I want to send the value of tag-select directly to controller every time it changes without javascript. Is that possible ?

@model MyViewModel
<form asp-controller="Home" asp-action="Create">
    <select asp-for="EmployeeId" asp-items="@(new SelectList(Model.EmployeesList,"Id","FullName"))">
        <option>Please select one</option>
    </select>
</form>


[HttpPost]
public IActionResult Create(MyViewModel model)
{
    //check model.EmployeeId 
    //to do : Save and redirect
}

回答1:

Try this:

<select asp-for="EmployeeId" asp-items="@(new SelectList(Model.EmployeesList,"Id","FullName"))" onchange="this.form.submit()">
    ...
</select>