在模特中,我有这个属性
public List<SelectListItem> listikOFProducts = new List<SelectListItem>();
我填写
var productList = from product in n.Products
select new { Name = product.ProductName, ID = product.ProductID };
foreach (var order in productList)
{
newOrder.listikOFProducts.Add(new SelectListItem { Text = order.Name, Value = order.ID.ToString() });
}
在视图中我显示它
<% using(Html.BeginForm("View2","Order"))
{ %>
<div>
<%: Html.DropDownList("drop",Model.listikOFProducts) %>
</div>
<input type="submit" name="button" value="Add" />
<% } %>
如何在控制器的下拉列表中获取所选项目的文本。我知道DropDownList中所选项目的值。
[HttpPost]
public ActionResult View2(NorthOrder q, string button)
{
if (button == "Add")
{
string strDDLValue = Request.Form["drop"].ToString();//ID of dropdown selected item
return View();
}
答案 0 :(得分:0)
在post方法上使用您的id再次获取值
[HttpPost]
public ActionResult View2(NorthOrder q,int drop, string button)
{
if (button == "Add")
{
string strDDLValue= db.Products.FirstOrDefault(_=>_.ProductID == drop).ProductName;
return View();
}
}