Editing MVC 5, EF 6 and Multirow (Batch)

Project: Enter a list of corporate card transactions and let the user complete the documentation to support each payment. Allow the user to save all changes at the same time versus one entry at a time.


@using (@Html.BeginForm("Index", "CorpCardTransactions", FormMethod.Post))
        <th>Card Holder</th>
        <th>Post Date</th>
        <th>Transaction Date</th>
        <th>Branch Code</th>
    @for (int i = 0; i < Model.Count; i++)
            <td>@Html.HiddenFor(m => m[i].ID)</td>
            <td>@Html.DisplayFor(m => m[i].AccountID)<br />
            @Html.DisplayFor(m => m[i].CardHolderName)</td>                
            <td>@Html.DisplayFor(m => m[i].PostDate)</td>
            <td>@Html.DisplayFor(m => m[i].TranDate)</td>
            <td>@Html.DisplayFor(m => m[i].Payee)</td>
            <td>@Html.DisplayFor(m => m[i].Amount)</td>
            <td>@Html.EditorFor(m => m[i].Description)</td>
            <td>@Html.EditorFor(m => m[i].GL_Account)</td>
            <td>@Html.EditorFor(m => m[i].BranchCode)</td>
            <td>@Html.EditorFor(m => m[i].Receipt)</td>
<p><input type="submit" value="Save Changes" /></p>
<p style="color:green; font-size:12px;">@ViewBag.Message</p>


Note that I have several DisplayFor cells that are used to simply display values ​​from the database without allowing the user to modify that data. Only the last 4 fields can be changed or updated.


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using intranetMVC.Models;    
using Microsoft.AspNet.Identity;

namespace intranetMVC.Controllers
public class CorpCardTransactionsController : Controller
    private ExpenseReportingEntities db = new ExpenseReportingEntities();

    public ActionResult Index(string AccountID, DateTime StatementDate)
        //var loginName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\');
        //var username = loginName.Last() + "@redcanoecu.com";
        List<CorpCardTransaction> model = new List<CorpCardTransaction>();
        var username = "mellis@redcanoecu.com";
        var stmtDate = StatementDate;
        var q = from b in db.CorpCardTransactions
                where b.Username == username && b.StatementDate == StatementDate && b.AccountID == AccountID && !b.SubmitDate.HasValue
                select b;
        model = q.ToList();
        return View(model);
    public ActionResult Index(List<CorpCardTransaction> list)
            using (db)
                foreach (var i in list)
                    var t = db.CorpCardTransactions.Where(a => a.ID.Equals(i.ID)).FirstOrDefault();
                    if(t != null)
                        t.Description = i.Description;
                        t.GL_Account = i.GL_Account;
                        t.BranchCode = i.BranchCode;
                        t.Receipt = i.Receipt;
            ViewBag.Message = "Successfully updated.";
                return View(list);
            ViewBag.Message = "Ooops, something went wrong.  Try again.";
            return View(list);



The problem is that when I return the "list" to the view after saving the changes, all the "DisplayFor" fields disappear and only the text fields with the changes that occurred appear. How can I get the whole view to display again like on page load? I tried to include them in the foreach statement, just making them equal to myself, but it doesn't work.

t.cardholdername = t.cardholdername


Is there a way to do like response.redirect () that I will use in webforms instead of doing Return View (list)? Thus, it will create the page the same as before the button was clicked, but it will include the new values.


source to share

1 answer


will not create an input element on the page, so the values ​​will not be sent back to the server. Add @Html.HiddenFor

for each of them and they will do it:

    @Html.DisplayFor(m => m[i].PostDate) 
    @Html.HiddenFor(m => m[i].PostDate)
    @Html.DisplayFor(m => m[i].TranDate) 
    @Html.HiddenFor(m => m[i].TranDate)


Or, if you saved StatementDate

, you can simply call your method Index

with these parameters. I would take this approach. It's simpler, less code, and feels more "correct".



All Articles