Night Channels
Night Channels

Reputation: 392

ASP.NET MVC Binding Value in View

Struggling to get a value from a drop down, to be passed into my controller for db inserting. It starts with a simple class for each item in the drop down.

 public class selectListUrgencyNum
{
    public string Text { get; set; }
    public string Value { get; set; }
}

Then in my view model I create a list of these objects as such..

public class createViewModel
{
    public Change Changevm { get; set; }

    public List<RequestType> rTypes { get; set; }

    public List<selectListUrgencyNum> listUrgency { get; set; }
}

Finally is my controller I populate this list to be displayed in the dropdown.

public ActionResult Create()
    {

        var urgNums = new List<selectListUrgencyNum>();
        for(int i = 1; i < 6; i++)
        {
            urgNums.Add(new selectListUrgencyNum() { Text = i.ToString(), Value = i.ToString() });
        }

        var viewModel = new createViewModel
        {
            listUrgency = urgNums 
        };



        return View(viewModel);
    }

I just used 1 - 5 as to make it simpler to understand.

My view creates the ddl like this.

  <div class="form-group">
        @Html.LabelFor(model => model.Changevm.UrgencyNum, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-2">
            <select class="form-control" id="select">
                <option> 1 = Least Urgent</option>
                @foreach(var item in Model.listUrgency)
                {
                    <option>@item.Text</option>
                }
                </select>
             @*How do I grab the value of what is selected??*@
          @Html.ValidationMessageFor(model => model.Changevm.UrgencyNum, "", new { @class = "text-danger" })
         </div>

So I'm not sure how to get the VALUE of what the selected drop down option is, so that on my insert I can access it. Possibly a hiddenfor?

And if I use a hidden for, how would it look?

If already answered please link..

Upvotes: 0

Views: 2241

Answers (2)

PatrykDz
PatrykDz

Reputation: 40

If you want to pass just Text and Value, you can use SelectListItem class from System.Web.Mvc, which is there for passing items to dropdown lists.

public class CreateViewModel
{
    public Change ChangeVm { get; set; }

    public List<RequestType> Types { get; set; }

    public List<SelectListItem> ListUrgency { get; set; }
}

In the Controller Create() method we create new ChangeVm also

var viewModel = new CreateViewModel()
{
    ChangeVm = new Change(),
    ListUrgency = urgNums
};

return View(viewModel);

On the View you can use Html.DropDownListFor extension method which will take care of creating the list with given items and proper name.

Html.BeginForm will take care of creating the form with proper "action" url, and method.

@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
<div class="form-group">
    @Html.LabelFor(model => model.ChangeVm.UrgencyNum, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-2">
        @Html.DropDownListFor(model => model.ChangeVm.UrgencyNum, Model.ListUrgency, "Choose Urgency...")
        @Html.ValidationMessageFor(model => model.ChangeVm.UrgencyNum, "", new { @class = "text-danger" })
    </div>
    <input type="submit" value="Submit"/>
</div>
}

In the controller you can then use

    [HttpPost]
    public ActionResult Create(CreateViewModel vModel)
    {
        if(vModel?.ChangeVm?.UrgencyNum != null)
        {
            var chosenValue = vModel.ChangeVm.UrgencyNum;
        }

        return View(vModel);
    }

If everything goes well, you should have your value chosen in vModel.ChangeVm.UrgencyNum

Upvotes: 1

M_Idrees
M_Idrees

Reputation: 2172

Add name attribute to your select.

<select class="form-control" id="select" name="selectedUrgency">

And within the posted action of the controller, lets say we have Create2 action method. We can retrieve value from Request object.

[HttpPost]
public ActionResult Create2()
{
   var value = Request["selectedUrgency"];
} 

Assuming that your given html has <form> tag with action url pointed to this action method.

Upvotes: 0

Related Questions