0

How do I append default value to the my dropdownlistfor using the code below

[AcceptVerbs(HttpVerbs.Get)]
    public ActionResult GetCitiesByStateId(int stateId)
    {
        if (stateId==0)
        {
            throw new ArgumentNullException("stateId");
        }

        var x = _repository.GetAllCitiesByStateId(stateId);
        var result = (from s in x
                      select new
                      {
                          id = s.id,
                          name = s.CityName
                      }).ToList();

        return Json(result, JsonRequestBehavior.AllowGet);
    }

My view code looks like this:

 @Html.DropDownListFor(x => x.City.ID, Model._Cities)
 @Html.ValidationMessageFor(x => x.City)

Thanks anticipation

codegrid
  • 977
  • 2
  • 13
  • 33
  • you haven't given us much to work with. you are returning json so you much be dynamically building the drop down on the view side. What does that code look like? if you are doing it something like this http://stackoverflow.com/questions/18351921/how-to-populate-a-cascading-dropdown-with-jquery then you just need to pass the desired default and in the loop set the checked attribute – Matt Bodily Feb 21 '14 at 19:20
  • Hi Matt, I added the view code. Thanks – codegrid Feb 22 '14 at 12:36
  • that is going to render as an html select list. the controller code you showed returns json implying that it gets called after the page loads. In that case you will need to manually build the drop down like the link I included above – Matt Bodily Feb 22 '14 at 18:02

0 Answers0