从View通过选择下拉列表中值控制器 [英] pass selected dropdown list value from View to Controller
本文介绍了从View通过选择下拉列表中值控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的视图传递参数(字符串)到我的控制器。该值应该是从视图中的下拉列表中选择。该方法被调用后一键点击,但在控制器的参数始终是空
I want to pass a parameter (string) to my Controller from my View. The value should be that which is selected from a dropdown list in the view. The method is called after a button-click, but in the Controller, the parameter is always null.
在查看:
@using (Html.BeginForm("Send", "Overview", FormMethod.Get))
{
<p>Email: @Html.DropDownList("Emails", Model.GetEmails())
<input type="submit" value="Send" />
</p>
}
在控制器:
public ActionResult Send(string email)
{
// email is always null!
return null;
}
在概览模式:
private List<SelectListItem> emails;
...
public Overview()
{
emails = new List<SelectListItem>();
emails.Add(new SelectListItem() { Text = "a@b.com", Value = "a@b.com", Selected = true });
emails.Add(new SelectListItem() { Text = "b@c.com", Value = "b@c.com", Selected = false });
emails.Add(new SelectListItem() { Text = "c@d.com", Value = "c@d.com", Selected = false });
}
...
public List<SelectListItem> GetEmails()
{
return emails;
}
能否请你帮我把这项工作?我在想什么?
(顺便说一句,心中永远的控制器,现在请返回null:)。)
Could you please help me get this work? What am I missing? (Btw, never mind the Controller returning null for now please :)).
推荐答案
您可以只使用一个串模型,
You could just use a string in your model,
public string emails { get; set; };
然后在您的视图使用 DropDownListFor
,
@Html.DropDownListFor(x => x.emails, Model.GetEmails())
和直接从在 POST
模型获得的价值。
and get the value directly from the model on the POST
.
[HttpPost]
public ActionResult Send(OverviewViewModel overviewModel)
{
//here is the value from the drop down
var theEmail = overviewModel.emails;
}
这篇关于从View通过选择下拉列表中值控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文