MVC 5,EF 6和Multirow(批量)编辑 [英] MVC 5, EF 6 and Multirow(batch) editing

查看:84
本文介绍了MVC 5,EF 6和Multirow(批量)编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目:引入公司卡交易的列表,并让用户填写文档以支持每次收费。让用户一次保存所有更改,而不是一次保存一个记录。

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.

视图

@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个字段。

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.

控制器

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);
        }
    }
}

}

问题是,当我保存更改后将列表返回到视图时,所有 DisplayFor字段都消失了,只有文本框包含发生的更改。如何像页面最初加载时一样再次显示整个视图?我试图将它们包括在foreach语句中,只是使其与自己相等,但这是行不通的。

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....
}

是否有一种方法可以像我在webforms中使用的response.redirect()一样,而不是做Return View(list)?这样,它将像在单击按钮之前一样构建页面,只包含新值。

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.

推荐答案

@ Html.DisplayFor 不会在页面,因此这些值不会被发布回服务器。为每个添加一个 @ Html.HiddenFor ,他们将对其进行备份:

@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>
...

或者,如果您保留 StatementDate ,则可以使用这些参数调用您的 Index 方法。我会采用这种方法。这样更容易,代码更少,感觉更正确。

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'.

这篇关于MVC 5,EF 6和Multirow(批量)编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆