How to edit a record in an SQL database from a View
<p>I want to preface this by saying that I am a complete and utter beginner to ASP.Net MVC programming. As such, I would appreciate explanations in a simple manner if possible, as I have very limited experience with this. I have a SQL database, with a table named <code>AtmAccounts</code>, in which I have stored some records (the only relevant column here being <code>AccountBalance</code>). </p>

<p>As part of my website, I have a ViewModel called <code>AtmAccountsVM</code>, defined below:</p>

<pre><code>public class AtmAccountVM
{
    public List&lt;AtmAccount&gt; 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; }
}    
</code></pre>

<p>I also have a view, <code>Deposit</code>. This view contains a textbox in which the user can enter an amount to deposit into their account, shown below.</p>

<pre><code>@using ATMApplication.ViewModels
@model ATMApplication.ViewModels.AtmAccountVM

@{
    ViewBag.Title = "Deposit";
 }

&lt;h2&gt;Deposit&lt;/h2&gt;

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


&lt;div class="form-group"&gt;
    @foreach (var item in Model.AtmAccountList)
    {
    @Html.DisplayNameFor(model=&gt;model.AtmAccountList.First().AccountBalance)

    @Html.DisplayFor(modelItem =&gt; item.AccountBalance)

    }

&lt;/div&gt;
&lt;div class="form-group"&gt;
    &lt;input type="submit" value="Deposit" class="btn btn-default" /&gt;

&lt;/div&gt;
</code></pre>

<p>Controller:</p>

<pre><code>[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=&gt;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 =&gt; a.UserId == userID).ToList();
        atmVm.UserName = User.Identity.GetUserName();

        return View(atmVm);
    }
}
</code></pre>

<p>With this, I am using the ViewModel above to display the <code>AccountBalance</code> value - I then want to update this value permanently in the SQL database through user input. How do I do this? Or am I going about this in the completely incorrect way?</p>

<p>UPDATE: New Post Method, flagging an error that reads"An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll but was not handled in user code"</p>

<pre><code> [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 =&gt; 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");
        }
</code></pre>