2017-02-28 59 views
0

我在從ASP.Net MVC窗體中獲取SelectListItem \ DropDwown選定值的值時遇到了一些困難。無法獲取SelectListItem DropDownFor選定的值

我檢查了過去的答案,並通過這些已經看過,並沒有成功

我也查了一些其他的答案太,我不能回去權現在

我的代碼在控制器中是

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,否則我無法獲得選定的值,除非必須這樣做,否則我不想這樣做,因爲我希望這樣可以將主控制器重定向到搜索控制器。

這是局部視圖

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

} 

的剃刀\ HTML - 編輯 - 查看型號上架

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

,請複製粘貼您的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" }) 
+0

我已經編輯的問題,以顯示視圖模型現在 –

+0

如果我是在同一個房間,我會吻你!我沒有嘗試的一件事,甚至沒有考慮..謝謝你的幫助 –