1

I am not able to save the record after implementing the drop down field. Can someone please correct my code.

Create.cshtml

    @model SomeIndianShit.Models.Accommodation

@{
    ViewBag.Title = "Advertise Accommodation";
}

<form name="datapluspics" method="post" enctype="multipart/form-data">
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Accommodation</legend>

<div class="editor-label">
            @Html.LabelFor(model => model.State)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.SelectedAustraliaStateId, Model.AustraliaStates)
        </div>
 <p>
            <input type="submit" value=" Save " />
        </p>
    </fieldset>

</form>

My Model:

public class AustraliaStates
    {
        [Key]
        public string AustraliaStateId { get; set; }
        public string AustraliaStateName { get; set; }
    }

public class Accommodation
    {
        [Key]
        public string A_Unique_Id { get; set; }

        [Display(Name = "Ad Id")]
        public string Ad_Id { get; set; }

        [Display(Name = "Posted By")]
        public string User { get; set; }

        [Display(Name = "Street")]
        public string Street { get; set; }

        [Required]
        [Display(Name = "Suburb")]
        public string Suburb { get; set; }

        [Required]
        [Display(Name = "State")]
        public string State { get; set; }

        public byte[] Picture1 { get; set; }



        public string SelectedAustraliaStateId { get; set; }
        public IEnumerable<SelectListItem> AustraliaStates { get; set; }
    }

AccommodationController.cs

// GET: /Accommodation/Create
        [Authorize]
        public ActionResult Create()
        {
            var model = new Accommodation
            {
                AustraliaStates = db.AustraliaStates
                             .ToList()
                             .Select(x => new SelectListItem
                             {
                                 Text = x.AustraliaStateName,
                                 Value = x.AustraliaStateId
                             })
            };

            return View(model);
        }

/ POST: /Accommodation/Create

[Authorize]
[HttpPost]
public ActionResult Create(Accommodation accommodation, HttpPostedFileBase file1, HttpPostedFileBase file2, HttpPostedFileBase file3)
{
    if (ModelState.IsValid)
    {
            // save and redirect
            // ...blah blah...
           //blah blah...

        db.Accommodation.Add(accommodation);
        //Save in Database
        db.SaveChanges();
        return RedirectToAction("Index");
    }


    // repopulate SelectList properties [I THINK THIS IS WRONG]
    var model = new Accommodation
    {
        AustraliaStates = db.AustraliaStates
                     .ToList()
                     .Select(x => new SelectListItem
                     {
                         Text = x.AustraliaStateName,
                         Value = x.AustraliaStateId
                     })
    };

    return View(accommodation);
}

After filling the form, when save button is clicked, the following error message is displayed

The ViewData item that has the key 'SelectedAustraliaStateId' is of type 'System.String' but must be of type 'IEnumerable'.

tereško
  • 58,060
  • 25
  • 98
  • 150
Reddy
  • 97
  • 1
  • 3
  • 10

1 Answers1

3

In your HttpPost controller action you need to repopulate the correct property on the model that you are passing to the view, i.e. set the AustraliaStates on your model the same way you are doing in your GET action:

accommodation.AustraliaStates = db.AustraliaStates
    .ToList()
    .Select(x => new SelectListItem
    {
        Text = x.AustraliaStateName,
        Value = x.AustraliaStateId
    });
return View(accommodation);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Used the above code, BUT my model is coming as INVALID if (ModelState.IsValid) -- showing as false – Reddy Jan 28 '14 at 10:48
  • That's because there are validation errors on the model. Make sure that the properties on your model decorated with the `[Required]` attribute have values. – Darin Dimitrov Jan 28 '14 at 11:27
  • I have entered valid data in all the available fields in the form but still not submitting the form. When I click on save button it just stays on the same page and model is coming as INVALID if (ModelState.IsValid) -- showing as false – Reddy Jan 28 '14 at 21:42
  • If I change Drop down filed to a text field and form is submitted successfully. – Reddy Jan 28 '14 at 21:44
  • @Reddy, there's no field in your form bound to the `State` and `Suburb` properties. Your dropdown is bound to `SelectedAustraliaStateId`. So you don't get any value for those required properties. – Darin Dimitrov Jan 29 '14 at 07:17