这是我的控制器:
public ActionResult Deposit()
{
return View();
}
[HttpPost]
public ActionResult Deposit(DepositTicket dt)
{
using (var db = new MatchGamingEntities())
{
MembershipUser currentUser = Membership.GetUser();
Guid UserId = (Guid)currentUser.ProviderUserKey;
var AccountId = from a in db.Accounts
where a.UserId == UserId
select a.AccountId;
BankTransaction transaction = new BankTransaction();
transaction.Amount = dt.Amount;
transaction.AccountId = AccountId.SingleOrDefault();
transaction.Created = DateTime.Today;
transaction.TransactionType = "Credit";
Debug.Write("Amount: " + transaction.Amount + " AccountId " + transaction.AccountId);
db.BankTransactions.AddObject(transaction);
db.SaveChanges();
return View();
}
}
这是我的观点:
@model MatchGaming.Models.DepositTicket
@{
ViewBag.Title = "Deposit";
}
<h2>Deposit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>DepositTicket</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Amount)
@Html.ValidationMessageFor(model => model.Amount)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
由于某种原因,在我的HttpPost中,尽管我在表单中输入了一个数字并提交了,但是我还是输入了一个数字,然后我将这个数字打印出来并且它始终为0.我不认为后期变量正在经历。< / p>
编辑:
DepostTicket类:
public class DepositTicket
{
[Required]
[Display(Name="Deposit Amount")]
[Range(5.00, 999999999999999999999999999.0, ErrorMessage = "Price must be above $5")]
public decimal Amount;
}
编辑2:
这是渲染的html
<form action="/Cashier/Deposit" method="post"> <fieldset>
<legend>DepositTicket</legend>
<div class="editor-label">
<label for="Amount">Amount</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Decimal must be a number." data-val-required="The Decimal field is required." id="Amount" name="Amount" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Amount" data-valmsg-replace="true"></span>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
答案 0 :(得分:2)
应该使用模型绑定器绑定的值必须是模型中的属性
public class DepositTicket
{
[Required]
[Display(Name="Deposit Amount")]
[Range(5.00, 999999999999999999999999999.0, ErrorMessage = "Price must be above $5")]
public decimal Amount { get; set; }
}