【问题标题】:Dropdown list edit review下拉列表编辑评论
【发布时间】:2016-12-23 10:37:33
【问题描述】:

所以我已经从我的数据库和从我生成视图的控制器中设置了我的模型和控制器。因此,为了给您一些背景信息,我创建了一个应用程序供用户发布和编辑有关特定服务的评论。我为用户创建了一个“创建”和“编辑”视图来创建评论和/或编辑评论。创建视图中的部分表单组允许用户选择他们想要查看的服务

<div>
                <div class="form-group">
                    @Html.LabelFor(model => model.WellnessService, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.DropDownList("WellnessService", new List<SelectListItem>
                {
                    new SelectListItem() {Text = "Student Wellness Service", Value = "Student Wellness Service"},
                    new SelectListItem() {Text = "HAICU", Value = "HAICU"},
                    new SelectListItem() {Text = "Student Counceling", Value = "Student Counceling"},
                    new SelectListItem() {Text = "DISCHO ( Discrimination and Harassment  Office)", Value = "DISCHO"},
                    new SelectListItem() {Text = "Campus Protection Service (CPS)", Value = "CPS"},
                    new SelectListItem() {Text = "Disablity Service", Value = "Disability Service"},
                }, "Select Wellness Service")



                        @Html.ValidationMessageFor(model => model.WellnessService, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>

但是在编辑视图中,用户可以根据自己的喜好调整服务的价值。我希望用户仅根据我提供的下拉菜单更改评论的服务名称,而不是输入

<div class="form-group">
                        @Html.LabelFor(model => model.Rating, htmlAttributes: new {@class = "control-label col-md-2"})
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Rating, new {htmlAttributes = new {@class = "form-control"}})
                            @Html.ValidationMessageFor(model => model.Rating, "", new {@class = "text-danger"})
                        </div>
                    </div>

如果有道理的话:P

我听说你必须在你的模型中设置一个枚举器,但是有可用的指南,这是代码优先 mvc5 而不是数据库优先

【问题讨论】:

    标签: c# asp.net-mvc ienumerable


    【解决方案1】:

    有很多方法可以做到这一点。 model 在您的 ViewModel 中,您可以创建一个 IEnumerable&lt;WellnessService&gt; WellnessService { get; set; } 并像这样传递它

    var services = new List <SelectListItem> {
        new SelectListItem() {
                Text = "Student Wellness Service", Value = "Student Wellness Service"
            },
            new SelectListItem() {
                Text = "HAICU", Value = "HAICU"
            },
            new SelectListItem() {
                Text = "Student Counceling", Value = "Student Counceling"
            },
            new SelectListItem() {
                Text = "DISCHO ( Discrimination and Harassment  Office)", Value = "DISCHO"
            },
            new SelectListItem() {
                Text = "Campus Protection Service (CPS)", Value = "CPS"
            },
            new SelectListItem() {
                Text = "Disablity Service", Value = "Disability Service"
            },
    }
    model.WellnessServices = services;
    

    或者你可以把它放在你的 ViewBag 中并像这样传递

    ViewBag.WellnessServices = services;
    

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 2021-06-12
      • 1970-01-01
      • 2016-05-21
      • 2015-09-13
      • 2011-06-16
      相关资源
      最近更新 更多