Asp.net mvc 4 下拉列表MVC4错误
我正试图得到一个下拉列表工作,但它不适合我。此应用程序主要是基于节日的应用程序,您可以在活动中添加节日。我遇到的错误是在线的:Asp.net mvc 4 下拉列表MVC4错误,asp.net-mvc-4,razor,html.dropdownlistfor,Asp.net Mvc 4,Razor,Html.dropdownlistfor,我正试图得到一个下拉列表工作,但它不适合我。此应用程序主要是基于节日的应用程序,您可以在活动中添加节日。我遇到的错误是在线的: @Html.DropDownList("towns", (IEnumerable<SelectListItem>)ViewData["Town"], new{@class = "form-control", @style="width:250px" }) 我怀疑当您将表单提交到[HttpPost]操作时,而不是在呈现表单时,会发生此错误,对吗?这个动作会呈
@Html.DropDownList("towns", (IEnumerable<SelectListItem>)ViewData["Town"], new{@class = "form-control", @style="width:250px" })
我怀疑当您将表单提交到
[HttpPost]
操作时,而不是在呈现表单时,会发生此错误,对吗?这个动作会呈现包含下拉列表的相同视图,对吗?在这个[HttpPost]
操作中,您忘记了像在HttpGet操作中一样填充ViewData[“Town”]
值,对吗
因此,继续并以与GET操作相同的方式填充此属性。当您向[HttpPost]
操作提交表单时,仅将所选值发送给控制器。因此,如果要重新显示同一视图,则需要重新填充集合值,因为此视图呈现一个下拉列表,试图从ViewData[“Town”]
绑定其值
以下是我对代码的理解:
[HttpPost]
public ActionResult SomeAction(Festival model)
{
... bla bla bla
// don't forget to repopulate the ViewData["Town"] value the same way you did in your GET action
// if you intend to redisplay the same view, otherwise the dropdown has no way of getting
// its values
ViewData["Town"] = ... same stuff as in your GET action
return View(model);
}
尽管如此,我还是强烈建议您使用视图模型,而不是ViewData/ViewBag弱类型的东西。不仅您的代码将变得更加干净,甚至错误消息也将开始有意义。啊,好的,我明白了,谢谢。在viewmodel中获取它容易吗?我才刚刚开始理解mvc,我试过了,现在我在代码背后的下拉列表中出现了一个错误。第195行:第195行:festival.FestivalTown.Name=fcFestival[“镇];这有什么问题?
//Get
List<SelectListItem> Towns = new List<SelectListItem>();
Towns.Add(new SelectListItem { Text = "Please select your Town", Value = "SelectTown" });
var towns = (from t in db.Towns select t).ToArray();
for (int i = 0; i < towns.Length; i++)
{
Towns.Add(new SelectListItem
{
Text = towns[i].Name,
Value = towns[i].Name.ToString(),
Selected = (towns[i].ID == 0)
});
}
ViewData["Town"] = Towns;
//Post
festival.FestivalTown.Town = collection["Town"];
public class Festival
{
public int FestivalId { get; set; }
[Required]
[Display(Name = "Festival Name"), StringLength(100)]
public string FestivalName { get; set; }
[Required]
[Display(Name = "Start Date"), DataType(DataType.Date)]
public DateTime StartDate { get; set; }
[Required]
[Display(Name = "End Date"), DataType(DataType.Date)]
public DateTime EndDate { get; set; }
[Required]
[Display(Name = "County")]
public virtual County FestivalCounty { get; set; }
[Display(Name = "Festival Location")]
public DbGeography Location { get; set; }
[Required]
[Display(Name = "Town")]
public virtual Town FestivalTown { get; set; }
[Required]
[Display(Name = "Festival Type")]
public virtual FestivalType FType { get; set; }
public UserProfile UserId { get; set; }
}
public class Town
{
public int ID { get; set; }
[Display(Name = "Town")]
public string Name { get; set; }
}
[HttpPost]
public ActionResult SomeAction(Festival model)
{
... bla bla bla
// don't forget to repopulate the ViewData["Town"] value the same way you did in your GET action
// if you intend to redisplay the same view, otherwise the dropdown has no way of getting
// its values
ViewData["Town"] = ... same stuff as in your GET action
return View(model);
}