从同一视图获取这些值后,从控制器的视图中传递值
Passing values in a view from controller after getting those values from same view
我是 MVC 的新手。我想从视图中获取 "fromDate" 和 "ToDate"。在同一个视图中,我需要三个空的和隐藏的DropDownLists。
点击提交按钮后,这些 DropDownLists 应该是可见的,并根据选择的日期填充数据。
但是我在查看页面上收到 'null reference' 错误。
我的浏览页面是
<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);
}
}
您的 GET 方法不会为属性 ProdNoList
、PartNoList
和 StatusList
初始化 SelectList
,因此当您尝试在 [=15= 中访问它们时] 抛出异常
您的 GET 方法将需要使用空列表添加初始化这些(例如使用 Enumerable.Empty<T>()
)
它不清楚你是如何 'hiding' 当你最初显示他的视图时,下拉列表,但你应该考虑一个视图模型 属性 说(bool DisplayDropDowns
你设置为 true
在返回视图之前在 POST 方法中。然后你可以使用
检查值
@if(Model.DisplayDropDowns)
{
.... // render dropdowns
}
这意味着 SelectList
不需要在 GET 方法中初始化,因为 if
块中的代码永远不会被执行。
另请注意,POST 方法应包括 return View(model);
我是 MVC 的新手。我想从视图中获取 "fromDate" 和 "ToDate"。在同一个视图中,我需要三个空的和隐藏的DropDownLists。
点击提交按钮后,这些 DropDownLists 应该是可见的,并根据选择的日期填充数据。
但是我在查看页面上收到 'null reference' 错误。
我的浏览页面是
<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);
}
}
您的 GET 方法不会为属性 ProdNoList
、PartNoList
和 StatusList
初始化 SelectList
,因此当您尝试在 [=15= 中访问它们时] 抛出异常
您的 GET 方法将需要使用空列表添加初始化这些(例如使用 Enumerable.Empty<T>()
)
它不清楚你是如何 'hiding' 当你最初显示他的视图时,下拉列表,但你应该考虑一个视图模型 属性 说(bool DisplayDropDowns
你设置为 true
在返回视图之前在 POST 方法中。然后你可以使用
@if(Model.DisplayDropDowns)
{
.... // render dropdowns
}
这意味着 SelectList
不需要在 GET 方法中初始化,因为 if
块中的代码永远不会被执行。
另请注意,POST 方法应包括 return View(model);