ViewData item key 'SelectedAustraliaStateId

2019-04-15 21:45发布

问题:

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'.

回答1:

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);