如何从视图

时间:2016-12-10 19:50:38

标签: c# asp.net-mvc asp.net-mvc-4

我想通过说我是ASP.Net MVC编程的完全和完全初学者来作为序言。因此,如果可能的话,我会以简单的方式感谢解释,因为我对此的经验非常有限。我有一个SQL数据库,其中有一个名为AtmAccounts的表,我在其中存储了一些记录(这里唯一相关的列是AccountBalance)。

作为我网站的一部分,我有一个名为AtmAccountsVM的ViewModel,定义如下:

public class AtmAccountVM
{
    public List<AtmAccount> AtmAccountList { get; set; }
    public string UserName { get; set; }

    public string FirstName { get; set; }
    public string Surname { get; set; }

    public string DepositAmount { get; set; }
    public string WithdrawAmount { get; set; }
}    

我也有一个观点,Deposit。此视图包含一个文本框,用户可以在其中输入存入其帐户的金额,如下所示。

@using ATMApplication.ViewModels
@model ATMApplication.ViewModels.AtmAccountVM

@{
    ViewBag.Title = "Deposit";
 }

<h2>Deposit</h2>

<p>Please input the amount you would like to deposit:</p>
<div class="form-group">
    <div>
   @Html.Label("Amount:")
    </div>
</div>
<div class="form-group">
    <div>
        @Html.TextBoxFor(model => Model.DepositAmount)
    </div>
</div>


<div class="form-group">
    @foreach (var item in Model.AtmAccountList)
    {
    @Html.DisplayNameFor(model=>model.AtmAccountList.First().AccountBalance)

    @Html.DisplayFor(modelItem => item.AccountBalance)

    }

</div>
<div class="form-group">
    <input type="submit" value="Deposit" class="btn btn-default" />

</div>

控制器:

[Authorize]
public class AtmAccountsController : Controller
{
    private BankDBEntities db = new BankDBEntities();

    // GET: AtmAccounts
    public ActionResult Index()
    {
        var userID = User.Identity.GetUserId();
        ViewModels.AtmAccountVM atmVm = new ViewModels.AtmAccountVM();
              atmVm.AtmAccountList = db.AtmAccounts.Where(a=>a.UserId==userID).ToList();
        atmVm.UserName = User.Identity.GetUserName();
        return View(atmVm);
    }

    // GET: AtmAccounts/Create
    public ActionResult Create()
    {
        ViewBag.AccTypeId = new SelectList(db.AccTypes, "Id", "AccountType");
        return View();
    }

    // POST: AtmAccounts/Create  
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,AccountNumber,AccountBalance,AccTypeId")] AtmAccount atmAccount)
    {
        if (ModelState.IsValid)
        {
            atmAccount.UserId = User.Identity.GetUserId();
            db.AtmAccounts.Add(atmAccount);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.AccTypeId = new SelectList(db.AccTypes, "Id", "AccountType", atmAccount.AccTypeId);
        return View(atmAccount);
    }

    //Get: Deposit
    public ActionResult Deposit()
    {
        var userID = User.Identity.GetUserId();
        ViewModels.AtmAccountVM atmVm = new ViewModels.AtmAccountVM();
        atmVm.AtmAccountList = db.AtmAccounts.Where(a => a.UserId == userID).ToList();
        atmVm.UserName = User.Identity.GetUserName();

        return View(atmVm);
    }
}

有了这个,我使用上面的ViewModel来显示AccountBalance值 - 然后我想通过用户输入在SQL数据库中永久更新这个值。我该怎么做呢?或者我是以完全错误的方式解决这个问题?

更新:新帖子方法,标记读取的错误&#34;类型&#39; System.Data.Entity.Validation.DbEntityValidationException&#39;的异常。发生在EntityFramework.dll中,但未在用户代码&#34;

中处理
 [HttpPost]
    public ActionResult Deposit(decimal DepositAmount, AtmAccount atmAccount)
    {


        using (var ctx = new BankDBEntities())
        {

            var userID = User.Identity.GetUserId();
            ViewModels.AtmAccountVM atmVm = new ViewModels.AtmAccountVM();
            DepositAmount = atmVm.DepositAmount;
            atmVm.AtmAccountList = db.AtmAccounts.Where(a => a.UserId == userID).ToList();
            atmVm.UserName = User.Identity.GetUserName();

            if (atmAccount.UserId == atmVm.UserName)
            {
                atmAccount.AccountBalance = atmAccount.AccountBalance + DepositAmount;
            }

            using (var dbCtx = new BankDBEntities())
            {
                dbCtx.Entry(atmAccount).State = System.Data.Entity.EntityState.Modified;
                dbCtx.SaveChanges();
            }

            return RedirectToAction("Index");
        }

1 个答案:

答案 0 :(得分:0)

您需要在表单中包含输入元素,并且您可以使用操作方法来处理表单提交。

@using(Html.BeginForm("Deposit","Account"))
{
 <div class="form-group">
    <div> @Html.Label("Amount:") </div>
</div>
<div class="form-group">
    <div> @Html.TextBoxFor(model => Model.DepositAmount)  </div>
</div>  
  <input type="submit" value="Deposit" class="btn btn-default" />

}

假设Deposit中有一个AccountController操作方法来处理表单提交

[HttpPost]
public ActionResult Deposit(decimal DepositAmount)
{
  // to do  : use the value and return something
}