[英]How do I return option value from DropDownList into Controller in ASP.NET
Here is the Razor code这是 Razor 代码
@Html.DropDownList("ddl", Model.estados.Select(item => new SelectListItem
{
Value = item.Id_Estado.ToString(),
Text = item.Nombre_Estado,
Selected = "select" == item.Id_Estado.ToString()
}), new { @class = "form-select", aria_label="Default select eaxmple" }
)
Here is the ViewModel, it's a IEnumarable这是 ViewModel,它是一个 IEnumarable
public class ViewModel
{
public UsuariosViewModel usuario { get; set; }
public IEnumerable<TiposUsuariosViewModel> tiposUsuarios { get; set; }
public IEnumerable<EstadosViewModel> estados { get; set; }
}
change the model更改 model
public class ViewModel
{
public string ddl {get; set;}
public IEnumerable<SelectListItem> estadoItems { get; set; }
public UsuariosViewModel usuario { get; set; }
public IEnumerable<TiposUsuariosViewModel> tiposUsuarios { get; set; }
public IEnumerable<EstadosViewModel> estados { get; set; }
}
in your action add estado items to a view model在您的操作中将 estado 项目添加到视图 model
estadoItems= context.estados.Select(item => new SelectListItem
{
Value = item.Id_Estado.ToString(),
Text = item.Nombre_Estado,
}).ToList();
viewModel.estadoItems = estadoItems
and view并查看
@Html.DropDownListFor(model=>model.ddl, @Model.estadoItems, new { @class = "form-select", aria_label="Default select example" });
but it is better to use select, since it is automatically selects item from list但最好使用 select,因为它会自动从列表中选择项目
<select class="form-control" asp-for="ddl" asp-items="@Model.estadoItems"> select </select>
I am assuming the you want to send the value of the selected option to your Controller
method.我假设您要将所选选项的值发送到您的
Controller
方法。 Now since you have not shown your Controller
method, I will give a basic example using AJAX
and Jquery
:现在由于您还没有展示您的
Controller
方法,我将给出一个使用AJAX
和Jquery
的基本示例:
First give an id
to your drop down list:首先给你的下拉列表一个
id
:
@Html.DropDownList("ddl", Model.estados.Select(item => new SelectListItem
{
Value = item.Id_Estado.ToString(),
Text = item.Nombre_Estado,
Selected = "select" == item.Id_Estado.ToString()
}), new { @class = "form-select", aria_label="Default select eaxmple", @id="myddl" }
)
You can have a button which will invoke the event or whatever event you are using, you can do that.您可以有一个按钮来调用事件或您正在使用的任何事件,您可以这样做。 I am using a button event here:
我在这里使用按钮事件:
<input type="button" value="Process Input" class="btn btn-primary btn-lg btn-block" id="mySubmitbtn" />
Then you can use AJAX to send it to your Controller
method and get a response back:然后您可以使用 AJAX 将其发送到您的
Controller
方法并得到回复:
$(document).ready(function () {
$("#mySubmitbtn").click(function () {
var mySelectedValue= $('#myddl').find(":selected").text();
var json = {
mySelectedValue: mySelectedValue
};
var options = {};
options.url = "@Url.Action("ProcessInput", "Home")";
options.type = "POST";
options.data = {"json": JSON.stringify(json)};
options.contentType = "application/json";
options.dataType = "json";
options.success = function (msg) {
alert("Successfully processed");
};
options.error = function () {
alert("Error");
};
$.ajax(options);
})
});
And finally your Controller
method will be:最后你的
Controller
方法将是:
using System.Web.Script.Serialization;
[HttpPost]
public JsonResult ProcessInput(string json)
{
var serializer = new JavaScriptSerializer();
dynamic jsondata = serializer.Deserialize(json, typeof(object));
//Get your variables here from AJAX call
var mySelectedValue = jsondata["mySelectedValue"];
//Do your stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.