从数据库MVC4 C#添加/获取值

时间:2013-05-19 10:49:31

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

我正在创建一项功能,允许用户向“瑞士银行”添加一定数量的资金。瑞士银行的余额存储在每个用户的dbidbalance)中。我坚持如何显示用户的当前余额以及如何添加或减少金额。我是c#mvc4的新人,所以非常感谢你的帮助(只是推动正确的方向会很棒)。

控制器:

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

}
}

型号:

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

查看:

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

1 个答案:

答案 0 :(得分:1)

步骤1:为创建创建操作方法。

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

第2步:添加具有相同名称的视图

步骤3:添加另一个操作方法以接收帖子数据并保存

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

阅读本文件:

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

它会帮助你。