[英]ASP.NET MVC 5 Set values for dropdownlist
I want to make a DropDownList with a numeric rating from 1-5. 我想制作一个数字等级为1-5的DropDownList。 I have a rating model and I want to apply these dropdown values to
WaitTime
, Attentive
and Outcome
. 我有一个评级模型,我想将这些下拉值应用于
WaitTime
, Attentive
和Outcome
。
Can I just set these values in the view and use the model? 我可以在视图中设置这些值并使用模型吗? If so how would i go about doing this?
如果是这样我将如何做到这一点?
My Model Class: 我的模型类:
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 : 我的观点 :
<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>
对每个字段使用此代替EditorFor:
@Html.DropDownListFor(model => model.Outcome, new SelectList(Enumerable.Range(1, 5)))
Here is the working fiddle - https://dotnetfiddle.net/daB2DI 这是工作小提琴 - https://dotnetfiddle.net/daB2DI
In short, lets say your model is - 简而言之,让我们说你的模型是 -
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; }
}
And your controller actions are - 你的控制器动作是 -
[HttpGet]
public ActionResult Index()
{
return View(new SampleViewModel());
}
[HttpPost]
public JsonResult PostData(SampleViewModel model)
{
return Json(model);
}
Your Get CSHTML should be - 你的获取CSHTML应该是 -
@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>
}
When you run the code, you should see page like below - 运行代码时,您应该看到如下页面 -
And when you select some values and click on create, you should get those values in PostData
action. 当您选择一些值并单击create时,您应该在
PostData
操作中获取这些值。
try to adapt this to your project in your controller: 尝试将其适应您控制器中的项目:
ViewBag.ParentID = new SelectList(departmentsQuery, "NewsMID", "Title", selectedDepartment);
return View(model);
in your view put this 在你看来放这个
@Html.DropDownList("ParentID")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.