Madhav Sharma

Madhav Sharma

  • 880
  • 899
  • 39.9k

Get dropdown selecte value in edit view page do manipulation

Mar 17 2017 9:36 AM
Controller
public ActionResult Index(int? id)         
{             
VarsityContext VarsityDB = new VarsityContext();             
Varsity.Models.Configuration configuration = new Varsity.Models.Configuration();
try             
{                              
if (id != null)                 
{                     
configuration = VarsityDB.Configurations.Single(a => a.Conf_ID == id);    
ViewBag.Action = "Edit";                    
}                 
else                
{                     
ViewBag.Action = "Create";                 
}                   
IEnumerable<SelectListItem> basetypes = new SelectList(cwConfiguration.getConfigurationTypes(), "Id", "Name", configuration.Conf_TypeName);                                  
ViewBag.Conf_Type = basetypes;                 
ViewBag.Conf_TypeName = basetypes;             
}
catch (Exception ex)             
{               
}             
return View(configuration);
}
View
@if (ViewBag.Action == "Edit")                            
{                                
@Html.DropDownList("Conf_Type", ViewBag.Conf_Type as SelectList, new { selected = @Model.Conf_TypeName, @class = "form-control" })                            
}                            
else                            
{                                
@Html.DropDownList("Conf_Type", ViewBag.Conf_Type as SelectList, "Select Type", new { required = "required", validationmessage = "Please select Type", selected = @Model.Conf_Type, @class = "form-control" })                            
}                            
@Html.HiddenFor(m => m.Conf_TypeName, ViewBag.Conf_TypeName as SelectListItem)

Attachment: images.rar

Answers (3)