我有两个文本框和一个下拉列表。我已经通过Ajax.BeginForm()方法使用它们的id将textboxes的值传递给controller action方法。但是如何传递dropdownlist的值,因为它没有由id定义。这是我的代码:
DeliveryIndex.cshtml:
height: 50px
控制器:
@using (Ajax.BeginForm("CustomerFilter", "Delivery", new System.Web.Mvc.Ajax.AjaxOptions
{
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "deliverylist"
}))
{
<input id="from" name="from" type="text" />
<input id="to" name="to" type="text" />
@Html.DropDownList("CUS_NAME",null,"--Select Customer--",new { @Style = "WIDTH:169PX" })
<input type="submit" value="View" id="BtnSubmit" />
}
答案 0 :(得分:3)
根据您的示例,参数应该可以CUS_NAME
。
将您的操作更改为:
[HttpPost]
public ActionResult CustomerFilter(string from, string to, string CUS_NAME)
{
....
}
将通过它,但我会建议使用强类型视图模型。
举个例子:
查看模型
public class FooViewModel
{
public string From { get;set; }
public string To { get;set; }
// represents the selected value
public string CusName { get;set; }
public List<Customer> AvailableCustomers { get;set;}
}
public class Customer
{
public int Id { get;set;}
public string Name { get;set;}
}
<强>动作强>
[HttpPost]
public ActionResult CustomerFilter(FooViewModel model)
{
....
}
查看强>
@model FooViewModel
@using (Ajax.BeginForm("CustomerFilter", "Delivery", new System.Web.Mvc.Ajax.AjaxOptions
{
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "deliverylist"
}))
{
@Html.TextBoxFor(m => m.From)
@Html.TextBoxFor(m => m.To)
@Html.DropDownListFor(m => m.CusName, new SelectList(model.AvailableCustomers, "Id", "Name"),"--Select Customer--",new { @Style = "WIDTH:169PX" })
<input type="submit" value="View" id="BtnSubmit" />
}
<强>更新强> 根据Stephen Muecke的评论,我已经更新了模型以包含一系列选项。