Simon Price
Simon Price

Reputation: 3261

Unable to get SelectListItem \ DropDownFor selected Value

I am having some difficulty in getting the value of a SelectListItem \ DropDwown selected value from an ASP.Net MVC form.

I have checked past answers and have looked through these and have no success

I have also checked some other answers too that I cant get back to right now

My Code in the controller is

public ActionResult QuickSearch()
    {
        var ddlValues = new List<DropDownValues>
        {
            new DropDownValues { Text= "5", Value = 5},
            new DropDownValues { Text= "10", Value = 5}
        };

        var model = new QuickSearchViewModel();
        model.Distance = ddlValues.Select(x => new SelectListItem {Text = x.Text, Value = x.Value.ToString() });

        //var model = new QuickSearchViewModel()
        //{
        //    Distance = from value in ddlValues
        //                  select new SelectListItem
        //                  {
        //                      Text = value.Text,
        //                      Value = value.Value.ToString()
        //                  }
        //};

        return PartialView(model);
    }

    [HttpPost]
    public ActionResult QuickSearch(QuickSearchViewModel model)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("QuickSearch", "Search", model);
        }
        return PartialView(model);
    }

The code to populate the Select List works perfectly fine, and the button that triggers the HttpPost event also works as expected.

But for the life of me I cannot get the selected Value out unless I use Ajax, which I dont want to do at this time unless I have to because I want this to redirect from the Home Controller to the Search Controller.

This is the Razor \ HTML of the partial view

@model SP.DABB.WebUI.Models.QuickSearchViewModel

@using(Html.BeginForm("QuickSearch", "Home", FormMethod.Post))
{
<div class="form-group">
    <div class="col-md-6 col-lg-6"> 
        @Html.LabelFor(x => x.PostCode)
    </div>
    <div class="col-md-6 col-lg-6">
        @Html.TextBoxFor(x => x.PostCode, new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    <div class="col-md-6 col-lg-6">
        @Html.LabelFor(m => m.Distance, new { @class = "control-label" })
    </div>
    <div class="col-md-6 col-lg-6">
        @Html.DropDownListFor(m => m.Distance, new SelectList(Model.Distance, "Value", "Text"), new { @class = "form-control" })
        @*@Html.DropDownListFor(m => m.Distance, Model.Distance, new { @class = "form-control" })*@
    </div>
</div>
<br /><br />
<div class="form-group">
    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 pull-right">
        @*<input type="button" id="bttnQuickSearch" class="btn btn-info pull-right" value="Perform Quick Search"/>*@
        <button type="submit" id="bttnQuickSearch" class="btn btn-info pull-right">Search</button>

    </div>
</div>

}

-- edit -- View Model Added

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SP.DABB.WebUI.Models
 {
public class QuickSearchViewModel
{
    [Display(Name = "Your Postcode")]
    [StringLength(maximumLength:8, MinimumLength = 5, ErrorMessage = "Please provide your full postcode.")]
    public string PostCode { get; set; }
    [Display(Name = "Distance")]
    public IEnumerable<SelectListItem> Distance { get; set; }

    public int SelectedDistance { get; set; }

    public QuickSearchViewModel()
    {
        Distance = new List<SelectListItem>();
    }
}
}

Any and all help is very much appreciated.

Upvotes: 0

Views: 1615

Answers (1)

faint220
faint220

Reputation: 111

Please paste code of your SP.DABB.WebUI.Models.QuickSearchViewModel

@EDIT

DropDownListFor first argument is a parameter that you want to bind selected value to. In this situation you can just change

 @Html.DropDownListFor(m => m.Distance, new SelectList(Model.Distance, "Value", "Text"), new { @class = "form-control" })

to:

 @Html.DropDownListFor(m => m.SelectedDistance, Model.Distance, new { @class = "form-control" })

Upvotes: 1

Related Questions