Hi Friends,in this article I will explain about How to Bind Data to DropDownList from Database using Stored Procedure
in MVC 4 Razor.
I already explained in the previous articles about How to bind DropDownList from database in C# MVC 4 razor,How to Create Cascading DropDownList in MVC 4 Razor and JQuery and MVC4 Razor:How to call Stored Procedure using C#
I already explained in the previous articles about How to bind DropDownList from database in C# MVC 4 razor,How to Create Cascading DropDownList in MVC 4 Razor and JQuery and MVC4 Razor:How to call Stored Procedure using C#
CREATE PROCEDURE Get_Name
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM student_data
order by
firstname Asc
END
GO
|
Account.cs Model:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MvcCRUD.Models
{
public
class Account
{
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
public DataSet
Bind2DDL()
{
StudentContext db = new
StudentContext();
SqlConnection con = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand cmd = new
SqlCommand("Get_Name",
con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new
SqlDataAdapter(cmd);
DataSet ds = new
DataSet();
da.Fill(ds);
return ds;
}
}
}
|
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
Bind2Ddl(Account obj)
{
DataSet ds = obj.Bind2DDL();
ViewBag.fname =
ds.Tables[0];
List<SelectListItem>
items = new List<SelectListItem>();
foreach (System.Data.DataRow
dr in ViewBag.fname.Rows)
{
items.Add(new SelectListItem
{ Text = @dr["firstname"].ToString(),
Value = @dr["firstname"].ToString()
});
}
ViewBag.firstname
= items;
return View();
}
}
}
|
Bind2Ddl.cshtml
@model MvcCRUD.Models.Account
@{
ViewBag.Title = "Bind Data to DropDownList from Database using
Stored Procedure in MVC 4 Razor";
}
<h3 style='color:Green'>Bind
Data to DropDownList from Database using Stored Procedure in MVC 4 Razor</h3><br/>
FirstName
@Html.DropDownList("firstname")
|
The output of the above code as shown in the below figure.
very nice article,easy to understand for asp.net developers
ReplyDeletevery good for new developer
ReplyDeleteThank You Ravi Shanker.
Deletevery helpful..
ReplyDeletehow can we bind multiple dropdown from database using postback in Html page
please suggest
Hi Tanuja,
DeleteThank You.
Refer below links
http://aspdotnet-kishore.blogspot.in/2013/10/how-to-create-cascading-dropdownlist-in.html
http://aspdotnet-kishore.blogspot.in/2013/10/json-create-cascading-dropdownlist-from.html
Азаматсын ким жазса да, Кыргыз балага жардам бердин.
ReplyDeleteit was helpful.
ReplyDeleteThanks kishore