将数据从硬编码下拉列表发送到控制器

时间:2012-03-25 00:10:24

标签: asp.net-mvc-3

从textboxe迁移到dropdownlist - 需要将值从硬编码的下拉列表发送到控制器

以下代码用于控制器

var list = new SelectList(new[]
                                      {
                                          new{ID="1",Name="20012"},
                                          new{ID="2",Name="20011"},
                                          new{ID="3",Name="20010"},
                                          new {ID="4",Name="2009"},
                                          new{ID="5",Name="2008"},
                                          new{ID="6",Name="2007"},
                                            new{ID="7",Name="2006"},
                                              new{ID="8",Name="2005"},
                                                new{ID="9",Name="2004"},
                                                  new{ID="3",Name="2003"},
                                                   new{ID="3",Name="2002"},
                                                    new{ID="3",Name="2001"},
                                                    new{ID="3",Name="2000"},

                                      },
                              "ID", "Name", 1);
        ViewData["list"] = listYear;

以下代码用于视图

@using (Html.BeginForm()){   
     <p>
  Title: @Html.TextBox("SearchString")  
        @Html.DropDownList("list",ViewData["list"] as SelectList) 
     Genre: @Html.DropDownList("Towns", "All")  
     <input type="submit" value="Filter" /></p>
    }

以下是用于文本框的代码

  if (!String.IsNullOrEmpty(year))
        {
            car = Cars.Where(s => s.Year.Contains(year));
        }

1 个答案:

答案 0 :(得分:2)

看起来您正在尝试从选择列表中选择一个值并将该值发送到控制器。首先,我建议您使用ViewModel而不是魔术字符串。您应修改视图以接受新的ViewModel,然后将模型发布到您的操作中。它简单,清洁,更易于维护。

以下是您的模型的样子

public class VehicleYearsViewModel {

    public SelectList VehicleYears { get; set; }
    public int SelectedYear { get; set; }

    public VehicleYearsViewModel() {
        VehicleYears = new SelectList(new[]
                                  {
                                      new{ID="1",Name="2012"},
                                      new{ID="2",Name="2011"},
                                      new{ID="3",Name="2010"},
                                      new{ID="4",Name="2009"},
                                      new{ID="5",Name="2008"},
                                      new{ID="6",Name="2007"},
                                      new{ID="7",Name="2006"},
                                      new{ID="8",Name="2005"},
                                      new{ID="9",Name="2004"},
                                      new{ID="3",Name="2003"},
                                      new{ID="3",Name="2002"},
                                      new{ID="3",Name="2001"},
                                      new{ID="3",Name="2000"}
                                  }
    }
}

您的视图将如此:

@YourAppName.Models.VehicleYearsViewModel

@using (Html.BeginForm()){
    @Html.ValidationSummary(true)
    @Html.DropDownListFor(model => model.SelectedYear, Model.VehicleYears, "ID", "Name", 1))
    <input type="submit" value="OK" />
}

您的控制器操作将接受该模型,并可以将所选值用作int数据类型。 我只是猜测你的控制器动作没有发布,但这几乎就是它的样子:

public class HomeController : Controller {

    public ActionResult Index() {
        var model = new VehicleYearsViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(VehicleYearsViewModel model) {
        if(ModelState.IsValid) {
            // you can get selected year like so
            int selectedYear = model.SelectedYear;
            // ... your code here to do whatever with selectedYear
        }
        return View(model);
     }   
}

希望这有帮助