Sharades
Sharades

Reputation: 3

Add/get value from Database MVC4 C#

I'm creating a feature which allows a user to add an certain amount of money to a 'Swiss bank'. The balance of the swiss bank is stored in the db (idand balance) for each user. I'm stuck on how to display the current balance of the user and how to add or witdraw an amount of money. I'm very new on c#and mvc4 so help is really appreciated (just a push in the right direction would be awesome).

Controller:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiveGangs.Models;

namespace FiveGangs.Controllers {
public class BankController : Controller {
    private FGEntities db = new FGEntities();
    //
    // GET: /SwissBank/

    [HttpGet]
    public ActionResult Index()
    {
        var gangster =
            db.UserGangster.FirstOrDefault(g => g.UserProfile.UserName == User.Identity.Name && g.Health > 0);
    }

    public ActionResult Index() {
        return View(db.SwissBank);
    }

}
}

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FiveGangs.Models
{
public partial class SwissBank {
    public int Id { get; set; }
    public string Balance { get; set; }
 }
 }

View:

    @using FiveGangs.Models
    @model System.Data.Entity.DbSet<SwissBank>

    @{
    ViewBag.Title = "Bank";
    }

<h2>Bank</h2>

<form>
  <fieldset>
    <legend>Swiss Bank</legend>
      <label>Balance: @String.Format("{0:C0}", @Model.Balance)</label>
      <span class="add-on">$</span>
      <input type="text" placeholder="Amount...">
      <span class="help-block">Withdrawing from your Swiss bank will cost you 25% of the amount of cash withdrawn!</span>
     <button class="btn" type="button">Deposit</button>
  <button class="btn" type="button">Withdraw</button>
  </fieldset>
</form>

Upvotes: 0

Views: 3934

Answers (1)

Bappi Datta
Bappi Datta

Reputation: 1380

Step 1: Create a Action Method for Create.

public ActionResult Create() {
    return View();
}

Step 2: Add View with same name

Step 3: Add Another Action Method to receive post data and save

[HttpPost]
public ActionResult Create(SwissBank swisBank) {
    if(ModelState.Isvalid)
    {
        db.SwisBank.Add(swisBank);
        db.SaceChanges()
    }
    return View();
}

Read this documents:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

It will help you.

Upvotes: 1

Related Questions