Posts

Showing posts from May, 2014

Using DropDownlist in MVC4

In MVC ddl Is Collection Of Selectlistitem Objects In create Method 1: In View:   @ Html.DropDownList( "BranchID" , String .Empty) In controller: ViewBag.BranchID = new SelectList (db.M_Branches, "ID" , "BranchName" ); Method 2: to get the “select” option in ddl In View:   @ Html.DropDownListFor(model => model.BranchID,    ( IEnumerable < SelectListItem >)ViewBag.BRANCHVAL, "Select" )   In controller:    ViewBag.BRANCHVAL = new SelectList (db.M_Branches, "ID" , "BranchName" ); In Index:   To bind ddl from different table/storeprocedure   Method 1: In View: ·           Using ViewBag @ Html.DropDownList( "ddllocation" , ( IEnumerable < SelectListItem >)ViewBag.location, "Select" )    Using  ViewData @ Html.DropDownList( "ddlDepartments" ,ViewData[ "Locations" ] as IEnumerable < SelectListItem >, "Select...

In Controller ----- ModelState.IsValid=false

You can Get Validation Error Messages from ModelState with using this function var query = from state in modelState.Values from error in state.Errors select error.ErrorMessage;  var errorList = query.ToList();  return errorList;