0

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.

  • 1
    Why are you submitting the form if the user is not editing or adding to that list. Your current code is simply printing the values on screen. There are no input form elements! – Shyju Aug 24 '17 at 15:33
  • i will change some value like isSended , isRejected after user submit it and i will update table with new values. – Melih Durak Aug 24 '17 at 15:39
  • Where are you changing ? Is it the user changing it in the UI ? then you need input form elements for that ( you do not have those now). If it is in the server code, i suggest read it again from the Db (the same way you read in GET action) and update as needed. – Shyju Aug 24 '17 at 15:53
  • Your form does not even contain any form controls, so even if you did generate the view correctly (see the dupe) there is nothing to post back so what are you trying to do here? –  Aug 24 '17 at 22:15
  • I'm getting data from database table that named TblExenseItem and show it to user who created them.User will see what s/he have and can edit if necessary with Edit/Delete actionlinks as you can see.After user hit the submit button, i'll assume that user confirmed the data and in the controller i'll change isSended areas of listed data value to 'true'.than i'm gonna udate table TblExpenseItem. @StephenMuecke – Melih Durak Aug 25 '17 at 06:48
  • User just see the list in that View.(user can edit if s/he wants with edit actionlink in there). after hitting the submit button, i'll change isSended value in my httppost action with foreach. @Shyju – Melih Durak Aug 25 '17 at 06:54

1 Answers1

1

Html.DisplayFor renders the data, but does not pass it back to the controller on submit. If you need data to be retained and sent back, you can use an Html.EditorFor and use the htmlAttributes to disable the component, or use the Html.DisplayFor method and pair it with an Html.HiddenFor which will store the value for submission.

Daniel
  • 1,695
  • 15
  • 33
  • i tried `Html.EditorFor` instead of `Html.DisplayFor` but i got nullReferenceException again. And i tried `Html.DisplayFor` with `Html.HiddenFor` but same error again. @Daniel – Melih Durak Aug 25 '17 at 06:34
  • 1
    @MelihDurak. Read the dupe I gave you! –  Aug 25 '17 at 06:50