2017-08-24 62 views
0

我在我的EmployeeController像ASP.NET MVC不能得到列出的數據從景觀到控制器

public ActionResult approvalList() 
     { 
      int sessionUser = Convert.ToInt32(Session["LogedUserID"]); 
      var listForApproval = db.TblExpenseItem.Where(k => k.UserID.Equals(sessionUser)); 

      return View(listForApproval.ToList()); 
     } 

我加入模板「列表」和模型類視圖「tableFromMyDB」。(我把一個動作@使用(Html.BeginForm)並自己提交按鈕。)

@model IEnumerable<ExpenseApplication.Models.TblExpenseItem> 

@{ 
    ViewBag.Title = "approvalList"; 
} 

<h2>approvalList</h2> 

<p> 
    @Html.ActionLink("Create New Expense", "ExpenseReport", "Employee") 
</p> 
@using (Html.BeginForm("approvalList", "Employee", FormMethod.Post)) 
{ 
    <table class="table"> 
     <tr> 
      <th> 
       @Html.DisplayNameFor(model => model.ExpenseDate) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.Description) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.Amount) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.isSended) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.isRejected) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.TblCategory.CtgName) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.TblUsers.UserName) 
      </th> 
      <th></th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      if (item.isSended == false) 
      { 
      <tr> 
       <td> 
        @Html.DisplayFor(modelItem => item.ExpenseDate) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.Description) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.Amount) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.isSended) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.isRejected) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.TblCategory.CtgName) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.TblUsers.UserName) 
       </td> 
       <td> 
        @Html.ActionLink("Edit", "Edit", new { id = item.ExpItemID }) | 
        @Html.ActionLink("Details", "Details", new { id = item.ExpItemID }) | 
        @Html.ActionLink("Delete", "Delete", new { id = item.ExpItemID }) 
       </td> 
      </tr> 
      } 
     } 
     <tr> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <th>Total Amount:</th> 
      <td> 
       @Model.Sum(x => x.Amount) 
      </td> 
     </tr> 

    </table> 
    <div class="col-md-offset-8 col-md-12"> 
     <input type="submit" value="Send List To Manager" class="btn btn-default" /> 
    </div> 
} 

httppost在我的控制器中的操作。

[HttpPost] 
     public ActionResult approvalList(List<TblExpenseItem> expItemList) 
     { 

      foreach (TblExpenseItem item in expItemList) 
      { 
       do stuff.. 
      } 
      int sessionUser = Convert.ToInt32(Session["LogedUserID"]); 
      expItemList = db.TblExpenseItem.Where(k => k.UserID.Equals(sessionUser)).ToList(); 


      return View(expItemList); 
     } 

當我點擊提交按鈕時,它在expItemList上拋出nullReferenceException。
我做了我的搜索,並找到了一些解決方案。但他們都關於創建,編輯等。
我只想列出來看看我的表中有什麼,然後提交它。之後,我會得到一些數據與foreach來自expItemList。
順便說一句我是新來的MVC,這是我的第一個問題,所以我很抱歉,如果我犯了什麼錯誤。

+1

如果用戶未編輯或添加到該列表,爲什麼要提交表單?您當前的代碼只是在屏幕上打印值。沒有輸入表單元素! – Shyju

+0

我會改變一些值,如isSended,isRejected用戶提交後,我會用新的值更新表。 –

+0

你在哪裏改變?用戶是否在UI中更改它?那麼你需要輸入表單元素(你現在沒有這些元素)。如果它在服務器代碼中,我建議再次從Db中讀取它(與您在GET操作中讀取的方式相同)並根據需要進行更新。 – Shyju

回答

1

Html.DisplayFor呈現數據,但不會在提交時將其傳遞迴控制器。如果您需要保留併發回數據,則可以使用Html.EditorFor並使用htmlAttributes來禁用該組件,或使用Html.DisplayFor方法並將其與Html.HiddenFor配對,該存儲器將存儲要提交的值。

+0

我嘗試過'Html.EditorFor'而不是'Html.DisplayFor',但我又得到了nullReferenceException。而且我用'Html.HiddenFor'嘗試了'Html.DisplayFor',但又出現了同樣的錯誤。 @Daniel –

+1

@MelihDurak。閱讀我給你的那個蠢貨! –

相關問題