【问题标题】:Create Dropdown field form from List with ASP.NET MVC使用 ASP.NET MVC 从列表创建下拉字段表单
【发布时间】:2019-11-25 09:56:18
【问题描述】:

在下一次,我可以创建一些帖子,因为我正在学习 C#ASP.NET MVC。我来自 Pythonic 世界,所以有些事情对我来说不是很清楚。

我想生成一个字符串列表,然后我想在我的表单中将这个列表显示为 DropDownList。

这是我的模型:

public class Joueur
    {
        public int ID { get; set; }

        [Required, Display(Name = "Nom"), StringLength(30)]
        public string Lastname { get; set; }

        [Required, Display(Name = "Prénom"), StringLength(30)]
        public string Firstname { get; set; }

        [Required, StringLength(15)]
        public string Poste { get; set; }

        public string Image { get; set; }
    }

根据创建方法,这是我的控制器:

// GET: Joueurs/Create
public ActionResult Create()
        {
            List<Strings> posteList = new List<SelectListItem>{ "Gardien", "Défenseur", "Milieu", "Attaquant" };
            ViewBag.PosteList = posteList;
            return View();
        }

这是我的观点:

<div class="col-md-10">
    @*ViewBag.PosteList is holding all the postes values*@
    @Html.DropDownListFor(model => model.Poste, ViewBag.PosteList as SelectList, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Poste, "", new { @class = "text-danger" })
</div>

但我遇到了这个问题:

@Html.DropDownListFor(model => model.Poste, ViewBag.PosteList as SelectList, new { @class= "form-control" })
没有类型为“IEnumerable”且键为“Poste”的 ViewData 元素。

我该怎么做?

使用 Django 非常简单,在我的模型中我创建了一个字典并在属性中传递了这个字典,但是使用 C# ASP.NET?我没有办法做到这一点。

【问题讨论】:

  • 您没有将Joueur 类作为模型发送到看起来的视图。有关示例,请参阅 https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller。

标签: c# asp.net asp.net-mvc


【解决方案1】:

我假设您的视图将显示一个表单来表示您希望用户填写的Joueur 对象,并且您的ViewBag.PosteList 将具有用户可以为Joueur.Poste 属性选择的值。为了实现这一点,您应该在您的 Create 控制器方法中创建一个新的/空的 Joueur 对象并将其传递给视图,如下所示:

public ActionResult Create()
{
    var model = new Joueur();
    List<Strings> posteList = new List<SelectListItem>{ "Gardien", "Défenseur", "Milieu", "Attaquant" };
    ViewBag.PosteList = posteList;
    return View(model);
}

那么你的原始代码的其余部分应该可以工作了。

【讨论】:

  • 这正是我想做的,但它不起作用。我找到了解决方案。这是我的编辑部分,但我没有进行迁移。当我学习 C# ASP.NET 和 Visual Studio 时,很多事情对我来说还不清楚。还是谢谢你
【解决方案2】:

我找到了解决办法,希望是个好办法:

在我的模型中,我创建了一个Enum

public class Joueur
    {
        public int ID { get; set; }

        [Required, Display(Name = "Nom"), StringLength(30)]
        public string Lastname { get; set; }

        [Required, Display(Name = "Prénom"), StringLength(30)]
        public string Firstname { get; set; }

        [Required, StringLength(15)]
        public Position Poste { get; set; }

        public string Image { get; set; }
    }

    public enum Position
    {
        Gardien,
        Défenseur,
        Milieu,
        Attaquant
    }

在我看来,我补充说:

<div class="form-group">
    @Html.LabelFor(model => model.Poste, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(m => m.Poste, new SelectList(Enum.GetValues(typeof(FCSL.Models.Joueur.Position))), "Sélectionner le poste", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Poste, "", new { @class = "text-danger" })
    </div>
</div>

我应用了迁移命令。现在好像可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2017-03-22
    • 2011-01-22
    • 1970-01-01
    • 2010-09-28
    相关资源
    最近更新 更多