gpt4 book ai didi

asp.net-mvc - MVC .NET Razor DropDownList

转载 作者:行者123 更新时间:2023-12-02 21:44:02 24 4
gpt4 key购买 nike

我无法将下拉列表链接到 View 中的模型。我有错误消息:具有键“title”的 ViewData 项的类型为“System.String”,但必须为“IEnumerable”类型使用以下代码:

public class FareCustomer
{
[Required]
public int title { get; set; }

我的 Controller :

List<SelectListItem> titleList = new List<SelectListItem>();
titleList.Add(new SelectListItem { Text = "Non renseigné", Value = "0" });
titleList.Add(new SelectListItem { Text = "Monsieur", Value = "1" });
titleList.Add(new SelectListItem { Text = "Madame", Value = "2" });
titleList.Add(new SelectListItem { Text = "Mademoiselle", Value = "3" });
ViewBag.title = titleList;
//Create a new customer
FareCustomer fareCustomer = new FareCustomer();
fareCustomer.title = 1;
return View("CreateAccount", fareCustomer);

我的 View “CreateAccount”

@model PocFareWebNet.Models.FareCustomer
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
@Html.DropDownList("title")
<input type="submit" value="Save" />
</fieldset>
}

我尝试应用此处描述的内容:Using the DropDownList Helper with ASP.NET MVC但显然我错过了一些东西。

最佳答案

尝试转换为 IEnumerable 并使用基于模型的帮助程序

@Html.DropDownListFor(m => m.title,((IEnumerable<SelectListItem>)ViewBag.title))

关于asp.net-mvc - MVC .NET Razor DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19862732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com