【发布时间】:2015-04-24 12:02:07
【问题描述】:
我是 MVC 的新手。我想从视图中获取“fromDate”和“ToDate”。在同一个视图中,我需要三个空且隐藏的 DropDownList。
点击提交按钮后,那些 DropDownLists 应该是可见的,并且根据选择的日期填充了数据。
但我在查看页面上收到“空引用”错误。
我的查看页面是
<h2>View Data in Database</h2>
<table><tr><td>
@using(Html.BeginForm("ViewPage1","Home"))
{
<table><tr><td>From Date:</td>
<td>@html.TextBoxFor(m=>m.FromDate,"{0:dd/MM/yyyy}")</td>
</tr>
<tr><td>To Date:</td>
<td>@html.TextBoxFor(m=>m.ToDate,"{0:dd/MM/yyyy}")</td>
</tr>
<tr><td><input type="submit" Value="Show"></td>
<td><input type="submit" Value="Show"></td>
</tr>
</table>
<div id="ShowDropBoxes">
<table>
<tr><td>@Html.CheckBox("Production Order:", new{id="ck1"})</td>
<td>@Html.DropDownlistFor(m=>m.ProdNo, Model.ProdOrdList, "Select Production Order")</td>
</tr>
<tr><td>@Html.CheckBox("Part Number:", new{id="ck2"})</td>
<td>@Html.DropDownlistFor(m=>m.PartNo, Model.PartNoList, "Select Part Number")</td>
</tr>
<tr><td>@Html.CheckBox("Status:", new{id="ck3"})</td>
<td>@Html.DropDownlistFor(m=>m.StatusTxt, Model.StatusList, "Select Status")</td>
</tr>
</table>
</div>
我的模特是
public class HomeModel
{
public DateTime FromDate {get; set; }
public DateTime ToDate {get; set; }
public string ProdNo {get; set; }
public string PartNo {get; set; }
public int status {get; set; }
public System.Web.Mvc.SelectList ProdNoList {get; set; }
public System.Web.Mvc.SelectList PartNoList {get; set; }
public System.Web.Mvc.SelectList StatusList {get; set; }
}
控制器是:-
public class HomeController: Controller
{
Repository List_in_Repository = new Repository();
public ActionResult ViewPage1()
{
return View();
}
[HttpPost]
public ActionResult ViewPage(HomeModel model)
{
string fromdate = model.FromDate.Tostring("yyyyMMdd");
string todate = model.ToDate.Tostring("yyyyMMdd");
model.ProdNoList = new SelectList(List_in_Repository.GetProductionOrders(fromdate,todate));
model.PartNoList= new SelectList(List_in_Repository.GetPartNumbers(fromdate,todate));
model.StatusList = new SelectList(List_in_Repository.GetStatus(fromdate,todate));
return View(model);
}
}
【问题讨论】:
-
写在 ViewPage 的末尾 return View(model);在 HttPost 方法中
-
你在哪里得到'空引用'? httppost 中的 return view() 在哪里?
标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4