I have customer list which is having all details like customerId,firstname,lastname
GetAllCustomer() method code
var customer = from d in dbContext.Customers
select d;
return customer.ToList();
In Index.cs I have bind firstname to dropdown list
var customerData =GetAllCustomer();
ViewBag.customerfirstname = new SelectList(customerData.Select(t=>t.firstname));
index.cshtml
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<p>
customer name : @Html.DropDownList("customerfirstname", "All")
<input type="submit" value="Filter" />
</p>
}
when I select any firstname from dropdown and click on submit I also want its customerId too.
How can I do it so?
You need to specify value field and text field for DropDownList, Now it will post CustomerID in for post against selected First Name :
var customerData =GetAllCustomer();
ViewBag.customerfirstname = new SelectList(customerData
.Select(
t=>new
{
FirstName = t.firstname,
CustomerID = customerId),
"CustomerID",
"FirstName");
and then in your View:
@Html.DropDownList("customerfirstname", "All")