ViewData项键'SelectedAustraliaStateId'的类型为'System.String',但必须是'IEnumerable <selectlistitem>'</selectlistitem>类型

时间:2014-01-28 03:55:52

标签: asp.net-mvc drop-down-menu

执行下拉字段后,我无法保存记录。有人可以请更正我的代码。

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>

我的模特:

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

填写表单后,单击“保存”按钮时,将显示以下错误消息

具有键'SelectedAustraliaStateId'的ViewData项的类型为'System.String',但必须是'IEnumerable'类型。

1 个答案:

答案 0 :(得分:3)

在您的HttpPost控制器操作中,您需要在传递给视图的模型上重新填充正确的属性,即在模型上设置AustraliaStates的方式与您在GET操作中的操作方式相同:

accommodation.AustraliaStates = db.AustraliaStates
    .ToList()
    .Select(x => new SelectListItem
    {
        Text = x.AustraliaStateName,
        Value = x.AustraliaStateId
    });
return View(accommodation);