0
 `       public ActionResult Edit(int id = 0)
    {
        property property = db.Properties.Find(id);

        try
        {
            ViewBag.City = new SelectList(db.Cities, "CityId", "City", property.CityId);
            ViewBag.Locality_Id = new SelectList(db.Areas, "Locality_Id", "Locality", property.Locality_Id);
            ViewBag.citylist = _data.getCity();
            ViewBag.typeproperty = _data.TypeProperty();
            ViewBag.Rooms = _data.RoomDetails();
            ViewBag.bath = _data.Bathroom();
            ViewBag.twowheel = _data.twowheeler();
            ViewBag.fourwheel = _data.fourwheeler();
            ViewBag.dir = _data.Direction();
            ViewBag.floor = _data.Flooring();
            ViewBag.water = _data.WaterSupplies();
            ViewBag.furnish = _data.Furnishes();
            ViewBag.Amenities = new SelectList(db.Amenities, "AmenitiesId", "Amenitie");
        }
        catch (Exception ex)
        {
           Console.Write( ex.InnerException.StackTrace.ToString());
        }



        if (property == null)
        {
            return HttpNotFound();
        }
        return View(property);
    }`    

 <div class="form-box-left">
                   <h5>* Select The City :</h5>
                 @Html.DropDownList("CityId", null, "Select City", new { @class = "text-box", Id = "City" })
                  @Html.ValidationMessageFor(model => model.CityId)
                   <h5>* Locality :</h5>

Same code I'm using for the create page and its working there but not on the edit page . I changed my code according to you but its still not submitting the data. While getting time it is ok.

Smoking monkey
  • 323
  • 1
  • 6
  • 22

2 Answers2

1

Following code is the example of using empty DropDownList:

@Html.DropDownList("CityId", Enumerable.Empty<SelectListItem>(), "Select City", new { @class = "text-box", Id = "City" })

To Populate a DropDownList with relevant data you may send data using ViewBag.

Example:

First, you need to sore list of city into ViewBag.City.

ViewBag.City = new SelectList(cityList, value, text, selectedCity)

Then you can use ViewBag.City into DropDownList.

@Html.DropDownList("CityId", ViewBag.City as SelectList, "Select City", new { @class = "text-box", Id = "City" })
Bappi Datta
  • 1,360
  • 8
  • 14
  • This way it is not selecting the city for the edit page. It is not even selecting the list of cities as well . – Smoking monkey Sep 02 '14 at 11:45
  • I want a particular city to be selected besides populating it with the list of city.. This way I am able to get the list of cities only. – Smoking monkey Sep 02 '14 at 12:33
  • @Smokingmonkey, The selected value will be the value of the property `CityId` so if you set the value `CityId = 5` in your model and you have a `SlectListItem` with the value of 5, then it will be selected. –  Sep 04 '14 at 10:20
0

You passed null as elements for Html.DropDownList. In such case engine picks elements from ViewData dictionary under key of "CityId". I assume there is an object of type Int32 there (i.e. ViewData["CityId"]). That's the default behavior.

Marcin Wachulski
  • 567
  • 4
  • 14