【问题标题】:Passing values in a view from controller after getting those values from same view [closed]从同一视图获取这些值后,从控制器传递视图中的值[关闭]
【发布时间】: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


【解决方案1】:

您的 GET 方法不会初始化属性 ProdNoListPartNoListStatusListSelectList,因此当您尝试在 @Html.DropDownlistFor(m =&gt; m.ProdNo, Model.ProdOrdList ...) 中访问它们时会引发异常。

您的 GET 方法将需要使用空列表添加初始化这些(例如使用 Enumerable.Empty&lt;T&gt;()

当您最初显示他的视图时,不清楚您是如何“隐藏”下拉列表的,但您应该考虑一个视图模型属性,例如(bool DisplayDropDowns,您在返回视图之前在 POST 方法中设置为true . 然后你可以使用检查值

@if(Model.DisplayDropDowns)
{
  .... // render dropdowns
}

这意味着 SelectList 不需要在 GET 方法中初始化,因为 if 块内的代码永远不会被执行。

另请注意,POST 方法应包含return View(model);

【讨论】:

  • 非常感谢斯蒂芬,你真的很棒......!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 2011-05-12
相关资源
最近更新 更多