无法获取 SelectListItem \ DropDownFor 选定值

Unable to get SelectListItem \ DropDownFor selected Value

我在从 ASP.Net MVC 表单中获取 SelectListItem \ DropDwown 选定值的值时遇到一些困难。

我检查了过去的答案并仔细查看了这些答案但没有成功

我也检查了一些其他答案,我现在无法返回

我在控制器中的代码是

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);
    }

填充 Select 列表的代码工作得很好,触发 HttpPost 事件的按钮也按预期工作。

但是对于我来说,除非我使用 Ajax,否则我无法获取选定的值,除非我必须这样做,否则我现在不想这样做,因为我希望它从家庭控制器重定向到搜索控制器。

这是局部视图的Razor \HTML

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

}

-- 编辑 -- 添加了视图模型

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>();
    }
}
}

非常感谢任何帮助。

请粘贴您的 SP.DABB.WebUI.Models.QuickSearchViewModel

代码

@编辑

DropDownListFor 第一个参数是您要将所选值绑定到的参数。在这种情况下你可以改变

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

至:

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