2
Reply

select value of drop-down should show in my Edit dropdown

Madhav Sharma

Madhav Sharma

Mar 16 2017 4:18 AM
215
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 (2)