Hi Friends ,in this article I will explain about How to bind DropDownList
from database in C# MVC 4 razor.
In previous artciles i already explained about How to Create Cascading DropDownList in MVC 4 Razor and JQuery,MVC4 Razor:How to call Stored Procedure using C# and MVC4 Razor :Routing or Set StartUp page in MVC4 with example
In previous artciles i already explained about How to Create Cascading DropDownList in MVC 4 Razor and JQuery,MVC4 Razor:How to call Stored Procedure using C# and MVC4 Razor :Routing or Set StartUp page in MVC4 with example
In Dropdownlist controller:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCRUD.Models;
using System.Data;
using System.Data.SqlClient;
namespace MvcCRUD.Controllers
{
public
class DropDownListController
: Controller
{
public
ActionResult Bind()
{
StudentContext db = new
StudentContext();
List<string>
x = new List<string>();
var firstname = from
fname in db.students
orderby
fname.ID
select fname.FIRSTNAME;
x.AddRange(firstname.Distinct());
ViewBag.firstname
= new SelectList(x);
List<string>
y = new List<string>();
var lastname = from
lname in db.students
orderby lname.ID
select lname.LASTNAME;
y.AddRange(lastname.Distinct());
ViewBag.lastname =
new SelectList(y);
return View();
}
}
}
|
In bind.cshtml:
@{
ViewBag.Title = "Bind";
}
<h2>Bind</h2>
FirstName
@Html.DropDownList("firstname")
LastName
@Html.DropDownList("lastname")
|
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog for more articles."
Hi, I am new bee to mvc4 but these tutorials gives much more confidence.
ReplyDeletePlease can you explain this:
what is StudentContext DB syntax please?
Hi Thanks.
DeleteStudentContext is a Data context class is nothig but connectionstring name in web.config file.When you add the below code in model
public class StudentContextDB : DbContext
{
public DbSet Students { get; set; }
}
then you acess like StudentContext db = new StudentContext();
If you really like my blog please share to your friends also.
Hi Kishore,
DeleteQuestion, I have a EmployeeController (has emp info) and DepartmentController (has dept info). Now on the EmployeeCreate. I want to have a DDL which has to be populated with DeptNames. How can I do this? on the .cshtml page I already have @model BusinessLayer.Employee, can I have a @model BusinessLayer.Department? i am not sure...
Can you post a sample example?
Appreciate your help,
Sam
Hi i will definitely write the article for above scenario.
DeleteThanks for reading my blog.
Hi I am new to MVC 4, and you are doing good and well in the examples but need to define each and every code part also you can provide the example code to download so every one can download and understand very well
ReplyDeleteHi Thanks for your Complement and Suggestion .Sure I will definitely improve in my next articles.I am very happy because my articles helps others for learning.Thanks all who read my articles.:)
DeleteI tried adding those lines in Model as you said..
ReplyDeleteBut I am getting error at db.Students..PLs help.
Awesome articles Kishore.Well presented..and Well Explained..Woth spending our time on thesr articles..
ReplyDeleteThank You Santosh Siva Kumar.
Delete