Thursday 24 October 2013

How to bind the data to DropDownList from database in ASP.NET MVC 4 razor using C#

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 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")

The output of the above code as shown in the below figure.


"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."

9 comments:

  1. Hi, I am new bee to mvc4 but these tutorials gives much more confidence.
    Please can you explain this:
    what is StudentContext DB syntax please?

    ReplyDelete
    Replies
    1. Hi Thanks.
      StudentContext 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.

      Delete
    2. Hi Kishore,
      Question, 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

      Delete
    3. Hi i will definitely write the article for above scenario.
      Thanks for reading my blog.

      Delete
  2. 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

    ReplyDelete
    Replies
    1. Hi 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.:)

      Delete
  3. I tried adding those lines in Model as you said..
    But I am getting error at db.Students..PLs help.

    ReplyDelete
  4. Awesome articles Kishore.Well presented..and Well Explained..Woth spending our time on thesr articles..

    ReplyDelete

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.