i have an action in my EmployeeController like
public ActionResult approvalList()
{
int sessionUser = Convert.ToInt32(Session["LogedUserID"]);
var listForApproval = db.TblExpenseItem.Where(k => k.UserID.Equals(sessionUser));
return View(listForApproval.ToList());
}
i added a view with template"List" and model class"tableFromMyDB".(i put @using(Html.BeginForm) and submit button myself. )
@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 action in my controller.
[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);
}
when i hit the submit button, it's throwing nullReferenceException on expItemList.
i did my search and found some solutions.But they all about create,edit etc..
I just wanna list to see what is in my table and then just submit it.After that, i will get some data with foreach from expItemList.
btw i'm new to MVC and it's my first question in there, so i'm sorry if i did any mistake.