Problem and Solutions

1

Like

How to Bind a DropDownlist in MVC Application.


Hi Friends

I need bind DropDownlist control in MVC Application. I am new in MVC please help me to do this.
Posted by: James
On 15 Apr 2012 | Views: 4996 | Categories: MVC
Solutions(3)
 On 15 Apr 2012 10:33:57 AM
Hi James

(1) First open MVC Project or Create New Project choose ASP.NET MVC 2 web app

(2) Add New Item in Model folder, Right click in Solutions Explorer Model folder >Add New Item Choose ADO.NET Entity Data Model through wizard add the database and table in your application and then

(3) Controller >HomeController.cs

namespace MvcBeginer.Controllers
{
[HandleError]
public class HomeController : Controller
{

EmployeeContainer EC=new EmployeeContainer();
public ActionResult Index()
{
return View();
}

public ActionResult About()
{
ViewData.Model = EC.Product_details.ToList();
return View();
}

public ActionResult ProductCategories()
{

var query = (from p in EC.Products select p).ToList();
var selectlist = new SelectList(query, "PId", "PCategory", 2);
ViewData["PCAT"] = selectlist;

return View();

}
// Add View page by right click on View>Home folder add view--//ProductCategories.aspx
}

}

// Now on ProductCategories.aspx

<%= Html.DropDownList("DropDownList1", (SelectList)ViewData["PCAT"])%>
 On 05 Sep 2012 05:38:55 AM
Good One...
 On 06 Aug 2013 10:30:14 PM
Thank you,
I want to ask some question.
When the page is two DropDownList, their data source are not same.
It can put-
ViewData["Drp1"] = selectlist1;
ViewData["Drp2"] = selectlist2;
and we can use it in aspx page like your example
<%= Html.DropDownList("DropDownList1", (SelectList)ViewData["Drp1"])%>
<%= Html.DropDownList("DropDownList2", (SelectList)ViewData["Drp2"])%>
pls reply.
Post your Solutions  

 

Other lins

.Net Techies
Other lins