0

I am new to MVC. I want to get "fromDate" and "ToDate" from a view. In the same view, I need three DropDownLists that are empty and hidden.

After clicking on the submit button, those DropDownLists should be visible and filled with data based on the dates picked.

But I am getting 'null reference' error on view page.

My View Page is

<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>

My Model is

  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; }
  }

Controller Is:-

  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);
       }
   }
TylerH
  • 20,799
  • 66
  • 75
  • 101
tanuj shrivastava
  • 722
  • 3
  • 9
  • 21

1 Answers1

0

Your GET method does not initialize the SelectList's for properties ProdNoList, PartNoList and StatusList so when you attempt to access them in @Html.DropDownlistFor(m => m.ProdNo, Model.ProdOrdList ...) the exception is thrown.

You GET method will need to add initialize these using empty lists (for example using Enumerable.Empty<T>())

Its not clear how you are 'hiding' the dropdown lists when you initially display he view, but you should consider a view model property say (bool DisplayDropDowns which you set to true in the POST method before returning the view. Then you could check the value using

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

This would mean the SelectList's would not need to be initialized in the GET method since the code inside the if block will never be executed.

Note also that the POST method should include return View(model);