我有兩個文本框和一個下拉列表。我已經通過Ajax.BeginForm()方法將文本框的值傳遞給控制器操作方法。但是如何傳遞dropdownlist的值,因爲它沒有通過id定義。這裏是我的代碼:使用ajax將DropDrownList中的值傳遞給控制器
DeliveryIndex.cshtml:
@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" />
}
控制器:
public class DeliveryController : MasterController
{
public ActionResult DeliveryIndex()
{
ViewBag.CUS_NAME = new SelectList(db.CUSTOMER_MASTER.ToList().OrderBy(c => c.CUS_NAME), "CUS_NAME", "CUS_NAME");
return View("DeliveryIndex");
}
[HttpPost]
public ActionResult CustomerFilter(string from, string to, string)
{
....
}
}
看起來好像你還沒有將它綁定到模型上。使用DropDownListFor並將其綁定到模型。您選擇的值將在您的模型中可用。 –