Reputation: 403
I have two textboxes and one dropdownlist. I have passed the value of textboxes to the controller action method using their id through Ajax.BeginForm() method. But how to pass value of dropdownlist as it has not defined by id. Here is my code:
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" />
}
Controller:
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 )
{
....
}
}
Upvotes: 2
Views: 182
Reputation: 33326
Based upon your example the parameter should be available as CUS_NAME
.
Changing your action to:
[HttpPost]
public ActionResult CustomerFilter(string from, string to, string CUS_NAME)
{
....
}
Will pass it through, however I would advise using a strongly typed view model.
As an example:
View Model
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;}
}
Action
[HttpPost]
public ActionResult CustomerFilter(FooViewModel model)
{
....
}
View
@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" />
}
Update As per Stephen Muecke's comments, I have updated the model to include a list of options too.
Upvotes: 3