Form values do not get transferred to HttpPost from View
Here is my Controller:
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();
}
}
Here is my 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>
}
For some reason in my HttpPost the DepositTicket dt.Amount is returning 0 even though I put a number in the form and submit, I printed the number out and its always 0. I dont think the post variables are going through.
EDIT:
DepostTicket Class:
public class DepositTicket
{
[Required]
[Display(Name="Deposit Amount")]
[Range(5.00, 999999999999999999999999999.0, ErrorMessage = "Price must be above $5")]
public decimal Amount;
}
EDIT 2:
Here is the rendered 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>
Values that are supposed to be binded using model binder have to be properties in model
public class DepositTicket
{
[Required]
[Display(Name="Deposit Amount")]
[Range(5.00, 999999999999999999999999999.0, ErrorMessage = "Price must be above $5")]
public decimal Amount { get; set; }
}
精彩评论