0

我正在创建一个功能,允许用户向“瑞士银行”添加一定数量的钱。瑞士银行的余额存储在每个用户的db(id和) 中。balance我被困在如何显示用户的当前余额以及如何添加或提取金额上。我是新手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>
4

1 回答 1

1

第 1 步:为 Create 创建一个 Action 方法。

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-应用

它会帮助你。

于 2013-05-19T11:27:27.173 回答