[RESOLVED]How to set dropdown value in mvc razor

How to set dropdown value in mvc razor

My code shown below

        tempagent Agentsobj = new tempagent();
                Agentsobj.AgentDDl = (from AgentsObj in realContext.Agents
                                      select new SelectListItem()
                                          Text = AgentsObj.Name,
                                          Value = SqlFunctions.StringConvert((double)AgentsObj.AgentId),

And my view

@Html.DropDownListFor(model => model.Agent, (IEnumerable<SelectListItem>)ViewBag.AgentDDl, new { @class = "form-control", id =Model.Agent })

in my model Agent contain Value 2 , I need to set drop down  of value 2

how to achieve this 

You mean to say you want to set a default value for your dropdown? or you want to achieve something else.

I need set dropdown list like text box ie

  var realContext = new RealEstateDBContext();
                PropertyDetails pp = realContext.PropertyDetails.SingleOrDefault(p => p.PropertyId ==id);
                PropertyVM vm = new PropertyVM();
                if (pp != null)
                    vm.Address = pp.Address;            //textbox value
                    vm.Agent = pp.Agent;        // dropdown value
                     vm.Apt = pp.Apt;          //textbox
  return View(vm);

your first lambda should be (model => model.AgentId, (IEnum etc etc),


Hi jishnuchandr,

Thanks for your post.

There is a lot of ways to bind dropdownlist in asp.net mvc (mvc3, mvc4 or later razor syntax .cshtml) and maintaining dropdownlist state during postback some of them given below -

  • Use viewbag property.
  • Through view model.
  • Use jquery .ajax i.e. asynchronous post back at selected index change of another dropdownlist.

More information:

binding dropdownlist in .net mvc razor by viewbag, model and jquery – 3 ways

Hope this can be helpful.

Best Regards,


Leave a Reply