Unable to pass data to view; Error: Does not conta

2019-08-25 07:00发布

问题:

I am trying to have a view that shows all work orders in the state of New Jersey. This is my work order model:

 public class WorkOrder
 { 
    public int UserId { get; set; }
    public string LocationId { get; set; }
    public string Reason { get; set; }
    public bool IsActive { get; set; } = true;
    public DateTime Date { get; set; } = DateTime.Now;
    public string StateId { get; set; } 
   }

In my View where I am trying to pass the data I have:

@model IEnumerable<WorkOrder>
@{
     ViewData["Title"] = "Work Orders";
 }
 <div class="by-location">
    <h1>By Location</h1>
   <ul>
       <li> @Html.ActionLink("Delaware", "Delaware", 
        "HomeController")</li>
       <li>
        @Html.ActionLink("New Jersey", "New Jersey", 
        "HomeController")
       </li>
       <li>
         @Html.ActionLink("Pennsylvania", "Pennsylvania", 
        "HomeController")
    </li>
  </ul>
</div>
<div class="container">
 <table class="table">
    <tr>
      <th>
        @Html.DisplayNameFor(model => model.UserId)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.LocationId)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.Reason)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.IsActive)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.Date)
      </th>
    </tr>
 @foreach (var item in Model)
 {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UserId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LocationId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Reason)
        </td>
        <td>

            @Html.DisplayFor(modelItem => item.IsActive)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StateId)
        </td>
     </tr>
    }
    </table>
  </div>

I am only trying to display the data where the location is equal to "NJ" so I have a method in my Controller that I am calling in my Controller that is supposed to return the View for New Jersey work orders but all has the logic for Getting work orders where the State = something.

   public IActionResult Nj(string stateId)
   {
       var njWorkOrders = GetWorkOrders()
          .Where(x => x.StateId == stateId).ToList(); 
       return View(njWorkOrders); 
   }

Here is my GetWorkOrders function:

  public List<WorkOrder> GetWorkOrders()
  {
      List<WorkOrder> workOrders = new List<WorkOrder>();
      workOrders.Add(new WorkOrder
      {
          UserId = 1,
          LocationId ="Philadelphia",
          Date = DateTime.Now,
          Reason = "Lights",
          IsActive = true,
          StateId = "PA"
      });

      workOrders.Add(new WorkOrder
      {
          UserId = 2,
          LocationId = "Camden",
          Date = DateTime.MinValue,
          Reason = "Plumbing",
          IsActive = true,
           StateId = "NJ"
      });
      workOrders.Add(new WorkOrder
      {
          UserId = 3,
          LocationId = "Burlington",
          Date = DateTime.Now,
          Reason = "Water",
          IsActive = false,
          StateId = "NJ"
      });

      workOrders.Add(new WorkOrder
      {
          UserId = 4,
          LocationId ="Wilmington",
          Date = DateTime.MaxValue,
          Reason = "Lights",
          IsActive = true,
          StateId = "DE"
      });

      // return the work orders to be used by your view methods
     return workOrders;
  }

回答1:

Few things:

  • You can't return IActionResult. You have to return ActionResult.

  • Since your controller action is expecting an argument, you have to pass the stateId in query string like below

http://localhost:52023/Home/Nj?stateId=NJ