我想问一个非常简单的问题,即我正在创建一个简单的MVC应用程序来测试下拉功能,控制器中有2个操作方法,一个用于GET,另一个用于POST。我想使用HTML表单助手回发从下拉列表中获取所选值。这是我的M,V和C;
型号:
public class Programming
{
public int selectedId { get; set; }
public List<SelectListItem> languanges;
}
控制器:
public class HomeController : Controller
{
private Programming programming;
private List<SelectListItem> list;
public HomeController()
{
programming = new Programming();
programming.languanges = new List<SelectListItem>();
list = new List<SelectListItem>() {
new SelectListItem(){ Value="1", Text="ActionScript"},
new SelectListItem(){ Value="2", Text="AppleScript"},
new SelectListItem(){ Value="3", Text="Asp"},
new SelectListItem(){ Value="4", Text="BASIC"},
new SelectListItem(){ Value="5", Text="C"},
new SelectListItem(){ Value="6", Text="C++"},
new SelectListItem(){ Value="7", Text="Clojure"},
new SelectListItem(){ Value="8", Text="COBOL"},
new SelectListItem(){ Value="9", Text="ColdFusion"},
new SelectListItem(){ Value="10", Text="Erlang"},
new SelectListItem(){ Value="11", Text="Fortran"},
new SelectListItem(){ Value="12", Text="Groovy"},
new SelectListItem(){ Value="13", Text="Haskell"},
new SelectListItem(){ Value="14", Text="instinctcoder.com"},
new SelectListItem(){ Value="15", Text="Java"},
new SelectListItem(){ Value="16", Text="JavaScript"},
new SelectListItem(){ Value="17", Text="Lisp"},
new SelectListItem(){ Value="18", Text="Perl"},
new SelectListItem(){ Value="19", Text="PHP"},
new SelectListItem(){ Value="20", Text="Python"},
new SelectListItem(){ Value="21", Text="Ruby"},
new SelectListItem(){ Value="22", Text="Scala"},
new SelectListItem(){ Value="23", Text="Scheme"},
};
}
// GET: Home
public ActionResult Index()
{
programming.languanges = list;
return View(programming);
}
[HttpPost]
public ActionResult Index(Programming programming)`enter code here`
{
return View();
}
}
查看:
@model BSTNTest.Models.Programming
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.DropDownListFor(c => c.selectedId, Model.languanges)
<button type="submit" class="btn btn-primary">Submit</button>
}
如果我在post动作中设置断点,我会将语言设为null!为什么会这样?
最后以null异常结束。
由于
答案 0 :(得分:1)
你必须在构造函数中将List不在Index方法中,而是在它的创建位置:
public HomeController()
{
programming = new Programming();
programming.languanges = new List<SelectListItem>();
list = new List<SelectListItem>() {
new SelectListItem(){ Value="1", Text="ActionScript"},
new SelectListItem(){ Value="2", Text="AppleScript"},
new SelectListItem(){ Value="3", Text="Asp"},
new SelectListItem(){ Value="4", Text="BASIC"},
new SelectListItem(){ Value="5", Text="C"},
new SelectListItem(){ Value="6", Text="C++"},
new SelectListItem(){ Value="7", Text="Clojure"},
new SelectListItem(){ Value="8", Text="COBOL"},
new SelectListItem(){ Value="9", Text="ColdFusion"},
new SelectListItem(){ Value="10", Text="Erlang"},
new SelectListItem(){ Value="11", Text="Fortran"},
new SelectListItem(){ Value="12", Text="Groovy"},
new SelectListItem(){ Value="13", Text="Haskell"},
new SelectListItem(){ Value="14", Text="instinctcoder.com"},
new SelectListItem(){ Value="15", Text="Java"},
new SelectListItem(){ Value="16", Text="JavaScript"},
new SelectListItem(){ Value="17", Text="Lisp"},
new SelectListItem(){ Value="18", Text="Perl"},
new SelectListItem(){ Value="19", Text="PHP"},
new SelectListItem(){ Value="20", Text="Python"},
new SelectListItem(){ Value="21", Text="Ruby"},
new SelectListItem(){ Value="22", Text="Scala"},
new SelectListItem(){ Value="23", Text="Scheme"},
};
programming.languanges = list;
}
请注意:你的post方法参数与全局变量同名,所以你要隐藏它。在Post方法中,您应该返回带有模型的视图以将列表放在那里。
[HttpPost]
public ActionResult Index(Programming par)
{
return View(programming);
}