Friday 15 March 2013

Create dropdownList in MVC with EntityFrameWork


This article will give's for   how to  create dropdownlist in mvc with Entity Frame Work?and how to  get the database table values then bind dropdownlist 

Create one class file(model)  and create  one property  for datatype is SelectList  in the class file for  look below way


public class RegisterModel
    {     
        public SelectList SchoolList { get; set; }     
        public int ScoolId { get; set; }
    }

This above model property is using for assign database return values then bind the property values in  dropdownlist . You can understand back


Next,Just call  database table(My Table name is School) values in one method for

SchoolBriefcaseEntities datamodel = new SchoolBriefcaseEntities();// It is my Entity class  object 


public IList<School> LoadSchools()
        {
            return datamodel.Schools.ToList();
        }


The above LoadScools method was  return some SchoolId and SchoolName(Its both of  my table column name's).



Then call the LoadSchool method for 


public SelectList BindSchools()
        {            
            IList<School> schools = LoadSchools(); 
            return new SelectList(schools, "SchoolId", "SchoolName"); 
        }

Then Just call  that RegisterModel and assign the database return values to model class(SchoolList) property  in inside of our Controller ActionResult  method  for 


[AllowAnonymous]
        public ActionResult Register()
        {
            RegisterModel registerModel = new RegisterModel();// It is Modelclass 
            registerModel.SchoolList = BindSchools();// assign the database return values in model class(SchoolList) property 
            return View(registerModel);
        }


Then, Right click inside of   the controller action result  method (Register)  -->Click Add View-->click the checkbox for create strongly _type view -->Select your modelclass in below dropdown (Now we have RegisterModel class .So we  choose  RegisterModel in this dropdownlist)--> Click Add Button .


Now the View(razor ) is created . you can see this view page in your solution explorer  .Its display like for 



@model SchoolBreifcase.Models.RegisterModel
@{
    ViewBag.Title = "Register"; 
}
<h2>
    Register</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>RegisterModel</legend>              
            <div class="editor-label">
                @Html.LabelFor(model => model.ScoolName)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.ScoolId, Model.SchoolList, "Select")
                @Html.ValidationMessageFor(model => model.ScoolName)
            </div>      
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


The above green line is called the RegisterModel class property(SchoolName). But we have already assigned some  values in this property  from database in controller . So now the dropdownlist is display our database values . you can get dropdown selected value in  ScoolId  property


If you have any doubt , then please comment or message me . 

My Mail Id is : 04RameshRajendran04@gmail.com                                   //You can send mail 

FaceBook page:https://www.facebook.com/DotnetSolutionsblogspotin      //You can post in wall 

or Comment please !







3 comments:

  1. Wow >>>. you are genus man......Thanks too mush v useful article

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, Awesome Post. Very nice information, I like it. Thanks for sharing good tips.
    DOT NET Training Course in Ameerpet
    DOT NET Training Institutes in Ameerpet

    ReplyDelete

If any doubt?then please comment in my post

How to reduce angular CLI build time

 Index: ----- I am using angular cli - 7 and I am going to tell how to reduce the build time as per my knowledge. Problem: -------- Now the ...