【问题标题】:Dropdown List MVC 4 error下拉列表 MVC 4 错误
【发布时间】:2014-02-10 15:47:57
【问题描述】:

我试图让一个下拉列表工作,但它不适合我。此应用程序主要是基于节日的应用程序,您可以在其中添加节日以及您的活动。我得到的错误在线:

@Html.DropDownList("towns", (IEnumerable<SelectListItem>)ViewData["Town"], new{@class = "form-control", @style="width:250px" })

这是我得到的错误:

没有具有键“城镇”的“IEnumerable”类型的 ViewData 项。

创建.cshtml

<div class="form-group">
@Html.LabelFor(model => model.FestivalTown, new { @class = "control-label col-md-2" })      
<div class="col-md-10">
@Html.DropDownList("towns", (IEnumerable<SelectListItem>)ViewData["Town"], new{@class = "form-control", @style="width:250px" })
@Html.ValidationMessageFor(model => model.FestivalTown)
</div>
@*@Html.Partial("ddlFestivalCounty");*@
</div>

Controller.cshtml

//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"];

模型.cs

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

【问题讨论】:

    标签: asp.net-mvc-4 razor html.dropdownlistfor


    【解决方案1】:

    我怀疑当您将表单提交到[HttpPost] 操作时会发生此错误,而不是在呈现表单时发生,对吧?这个动作会渲染包含下拉菜单的同一个视图,对吧?在这个[HttpPost] 操作中,您忘记填充ViewData["Town"] 值,就像您在HttpGet 操作中所做的那样,对吧?

    所以,继续以与 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 弱类型的东西。不仅您的代码会变得更加干净,而且甚至错误消息也会开始变得有意义。

    【讨论】:

    • 好吧,我明白了,谢谢。在视图模型中很容易获得它吗?我才刚刚开始了解 mvc。
    • 我试过了,现在我的下拉列表有错误,在后面的代码中。第 195 行: 第 195 行:festival.FestivalTown.Name = fcFestival["Town"];这有什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多