ASP.. NET MVC DropDownListFor在post返回时返回null
本文关键字:返回 null post NET MVC DropDownListFor ASP | 更新日期: 2023-09-27 18:12:33
我想问一个非常简单的问题,那就是我正在创建一个简单的MVC应用程序来测试下拉功能,控制器中有2个动作方法一个是GET,另一个是POST。我想从下拉列表中选择的值使用HTML表单助手post回来。这是我的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异常结束。
谢谢
您必须在Index方法中传递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"},
};
programming.languanges = list;
}
要小心:你的post方法参数与你的全局变量同名,所以你隐藏了它。在Post方法中,你应该返回带有模型的视图,以便在那里有列表。
[HttpPost]
public ActionResult Index(Programming par)
{
return View(programming);
}