项目:引入公司卡交易 list ,让用户填写文件以支持每次收费。让用户一次保存所有更改而不是一次保存一个记录。
风景
@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>
}
请注意,我有几个“DisplayFor”单元格,用于仅显示数据库中的值,而不允许用户更改该数据。只能修改或更新最后 4 个字段。
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 = "[email protected]";
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);
}
}
}
}
问题是,当我在保存更改后将“列表”返回到 View 时,所有“DisplayFor”字段都消失了,只有文本框出现了发生的更改。如何让整个 View 像最初加载页面时一样再次显示?我试图将它们包含在 foreach 语句中,只是让它们等于自己,但它不起作用。
{
t.cardholdername = t.cardholdername
etc....
}
有没有办法像我在网络表单中使用的 response.redirect() 而不是执行返回 View (列表)?这样它就会像在单击按钮之前一样构建页面,只是它会包含新值。
最佳答案
@Html.DisplayFor
不会在页面上创建输入元素,因此值不会被回发到服务器。为每个人添加一个 @Html.HiddenFor
,他们会备份它:
...
<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>
...
或者,如果您保留了
StatementDate
,则可以使用这些参数调用 return Index
方法。我会采取这种方法。它更容易,代码更少,感觉更“正确”。关于c# - MVC 5、EF 6 和 Multirow(batch) 编辑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25793531/