[英]ASP.NET MVC 5 Set values for dropdownlist
我想制作一個數字等級為1-5的DropDownList。 我有一個評級模型,我想將這些下拉值應用於WaitTime
, Attentive
和Outcome
。
我可以在視圖中設置這些值並使用模型嗎? 如果是這樣我將如何做到這一點?
我的模型類:
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; }
}
我的觀點 :
<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)))
這是工作小提琴 - https://dotnetfiddle.net/daB2DI
簡而言之,讓我們說你的模型是 -
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; }
}
你的控制器動作是 -
[HttpGet]
public ActionResult Index()
{
return View(new SampleViewModel());
}
[HttpPost]
public JsonResult PostData(SampleViewModel model)
{
return Json(model);
}
你的獲取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>
}
運行代碼時,您應該看到如下頁面 -
當您選擇一些值並單擊create時,您應該在PostData
操作中獲取這些值。
嘗試將其適應您控制器中的項目:
ViewBag.ParentID = new SelectList(departmentsQuery, "NewsMID", "Title", selectedDepartment);
return View(model);
在你看來放這個
@Html.DropDownList("ParentID")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.