首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ASP.NET MVC 5设置下拉列表的值

ASP.NET MVC 5设置下拉列表的值
EN

Stack Overflow用户
提问于 2015-06-06 10:32:01
回答 3查看 3.8K关注 0票数 3

我想做一个从1-5的数字评级的DropDownList。我有一个评级模型,我想将这些下拉值应用于WaitTimeAttentiveOutcome

我可以在视图中设置这些值并使用模型吗?如果是这样,我该怎么做呢?

我的模型类:

代码语言:javascript
复制
     public class Ratings
    {
        //Rating Id (PK)
        public int Id { get; set; }


        public string UserId { get; set; }

                     //Medical Practice (FK)
                    public int MpId { get; set; }
                     public MP MP { get; set; }

        //User ratings (non-key values)

        [Required] //Adding Validation Rule
        public int WaitTime { get; set; }

        [Required] //Adding Validation Rule
        public int Attentive { get; set; }

        [Required] //Adding Validation Rule
        public int Outcome { get; set; }


    }

My View

代码语言:javascript
复制
 <div class="form-group">
        @Html.LabelFor(model => model.WaitTime, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.WaitTime, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.WaitTime, "", new { @class = "text-danger" })
        </div>
    </div>

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

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

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-06-06 11:05:46

对于每个字段,请使用此方法而不是EditorFor:

代码语言:javascript
复制
@Html.DropDownListFor(model => model.Outcome, new SelectList(Enumerable.Range(1, 5)))
票数 3
EN

Stack Overflow用户

发布于 2015-06-06 11:07:12

这是工作的小提琴- https://dotnetfiddle.net/daB2DI

简而言之,让我们说你的模型是-

代码语言:javascript
复制
public class SampleViewModel
{
    [Required] //Adding Validation Rule
    public int WaitTime { get; set; }

    [Required] //Adding Validation Rule
    public int Attentive { get; set; }

    [Required] //Adding Validation Rule
    public int Outcome { get; set; }
}

你的控制员的行为是-

代码语言:javascript
复制
[HttpGet]
public ActionResult Index()
{
    return View(new SampleViewModel());
}


[HttpPost]
public JsonResult PostData(SampleViewModel model)
{               
    return Json(model);
}

你的CSHTML应该是-

代码语言:javascript
复制
@model HelloWorldMvcApp.SampleViewModel

@{
    ViewBag.Title = "GetData";
}

<h2>GetData</h2>
@{
    var items = new List<SelectListItem>();
    for (int i = 1; i < 6; i++)
    {
        var selectlistItem = new SelectListItem();
        var code = 0;
        selectlistItem.Text = (code + i).ToString();
        selectlistItem.Value = (code + i).ToString();
        items.Add(selectlistItem);
    }
}

@using (Html.BeginForm("PostData","Home")) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>SampleViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.WaitTime, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.WaitTime, items, "--Select--", new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.WaitTime, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

当你运行代码时,你应该看到下面的页面-

当您选择一些值并单击create时,您应该在PostData操作中获得这些值。

票数 1
EN

Stack Overflow用户

发布于 2015-06-06 11:05:54

尝试将其调整到控制器中的项目中:

代码语言:javascript
复制
    ViewBag.ParentID = new SelectList(departmentsQuery, "NewsMID", "Title", selectedDepartment);
  return View(model);

在你看来

代码语言:javascript
复制
  @Html.DropDownList("ParentID")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30681769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档