ASP.NET MVC 5 Set values ββfor dropdown
I want to make a DropDownList with a numeric rating from 1 to 5. I have a rating model and I want to apply those dropdown values ββto WaitTime
, Attentive
and Outcome
.
Can I just set these values ββin the view and use the model? If so, how can I do 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>
source to share
Here is a working fiddle - https://dotnetfiddle.net/daB2DI
In short, 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 actions with the controller are -
[HttpGet]
public ActionResult Index()
{
return View(new SampleViewModel());
}
[HttpPost]
public JsonResult PostData(SampleViewModel model)
{
return Json(model);
}
Your CSHTML should be -
@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 running the code, you should see a page as shown below -
And when you select some values ββand click on create, you should get those values ββin action PostData
.
source to share