问题
Project: bring in a list of corporate card transactions and let the user fill in documentation to support each charge. Let user save all changes at once versus one record at a time.
The View
@using (@Html.BeginForm("Index", "CorpCardTransactions", FormMethod.Post))
{
<table>
<tr>
<th></th>
<th>Card Holder</th>
<th>Post Date</th>
<th>Transaction Date</th>
<th>Payee</th>
<th>Amount</th>
<th>Description</th>
<th>GL/Account</th>
<th>Branch Code</th>
<th>Receipt</th>
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<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>
</tr>
}
</table>
<p><input type="submit" value="Save Changes" /></p>
<p style="color:green; font-size:12px;">@ViewBag.Message</p>
}
Notice that I have several "DisplayFor" cells that are used to just display the values from the database without allowing the user to change that data. Only the last 4 fields can be modified or updated.
The Controller
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);
}
[HttpPost]
public ActionResult Index(List<CorpCardTransaction> list)
{
if(ModelState.IsValid)
{
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;
}
}
db.SaveChanges();
}
ViewBag.Message = "Successfully updated.";
return View(list);
}
else
{
ViewBag.Message = "Ooops, something went wrong. Try again.";
return View(list);
}
}
}
}
The problem is that when I return "list" to the view after saving changes, all the "DisplayFor" fields disappear and only the text boxes are there with the changes that occurred. How can I get the entire view to display again like it does when the page loads initially? I've tried to include them in the foreach statement just making them equal to themselves but it doesn't work.
{
t.cardholdername = t.cardholdername
etc....
}
Is there a way to do like a response.redirect() that I would use in webforms instead of doing the Return View(list)? That way it'll build the page like it does before the button is clicked only it'll include the new values.
回答1:
@Html.DisplayFor
won't create an input element on the page, so the values won't get posted back to the server. Add a @Html.HiddenFor
for each one and they'll make it back up:
...
<td>
@Html.DisplayFor(m => m[i].PostDate)
@Html.HiddenFor(m => m[i].PostDate)
</td>
<td>
@Html.DisplayFor(m => m[i].TranDate)
@Html.HiddenFor(m => m[i].TranDate)
</td>
...
Or, if you kept the StatementDate
, you can just call your return your Index
method with those parameters. I'd take that approach. It's easier, less code, and feels more 'correct'.
来源:https://stackoverflow.com/questions/25793531/mvc-5-ef-6-and-multirowbatch-editing