ViewData项目键"SelectedAustraliaStateId"的类型为"System.String",但必须类型为"IEnumerable< SelectListItem>" [英] ViewData item key 'SelectedAustraliaStateId' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'
问题描述
实施下拉字段后,我无法保存记录.有人可以更正我的代码吗?
I am not able to save the record after implementing the drop down field. Can someone please correct my code.
Create.cshtml
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
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:/住宿/创建
/ 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
具有键"SelectedAustraliaStateId"的ViewData项的类型为"System.String",但必须类型为"IEnumerable".
The ViewData item that has the key 'SelectedAustraliaStateId' is of type 'System.String' but must be of type 'IEnumerable'.
推荐答案
在HttpPost控制器操作中,您需要在传递给视图的模型上重新填充正确的属性,即在模型上设置AustraliaStates
与您执行GET操作时相同的方式:
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);
这篇关于ViewData项目键"SelectedAustraliaStateId"的类型为"System.String",但必须类型为"IEnumerable< SelectListItem>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!