3

I want to make a DropDownList with a numeric rating from 1-5. I have a rating model and I want to apply these dropdown values to WaitTime, Attentive and 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>
1
  • 1
    Add a SelectList property to your model and populate it with 5 SelectListItem objects, the use @Html.DropDowListFor() in the view Commented Jun 6, 2015 at 10:35

3 Answers 3

3

Use this instead of EditorFor for each field:

@Html.DropDownListFor(model => model.Outcome, new SelectList(Enumerable.Range(1, 5)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @ranquild! This was simple and easy. worked like a charm
Or use the following syntax for Create/Edit views (does anybody know if it is possible to let it use the syntax from the validation?): <input asp-for="PersonalRating" class="form-control" type="range" min="1" max="10" />
1

Here is the working fiddle - 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 -

@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 -

enter image description here

And when you select some values and click on create, you should get those values in PostData action.

Comments

0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.