无法获取SelectListItem \ DropDownFor选定的值

时间:2017-02-28 19:18:19

标签: c# asp.net asp.net-mvc razor selectlistitem

我在从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);
    }

填充选择列表的代码完全正常,触发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>();
    }
}
}

非常感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:1)

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

的代码

@EDIT

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