我是MVC的新手,所以请保持温和。
我有模特 -
public class LoanPayments
{
public int PaymentNumber { get; set; }
public string PaymentDate { get; set; }
public double PaymentAmount { get; set; }
}
public class IndexModel
{
[Required]
[Display(Name = "Vehicle Price")]
public Double VehiclePrice { get; set; }
[Required]
[Display(Name = "Deposit Amount")]
public Double DepositAmount { get; set; }
[Required]
[Display(Name = "Delivery Date")]
public DateTime DeliveryDate { get; set; }
[Required]
[Display(Name = "Finance Option")]
public string FinanceOption { get; set; }
public IEnumerable<SelectListItem> Options { get; set; }
....
我有一个控制器 -
public class HomeController : Controller
{
public ActionResult Index()
{
// Let's get all states that we need for a DropDownList
var options = GetAllOptions();
var tuple = new Tuple<IndexModel, DateTime>(new IndexModel(), new DateTime());
// Create a list of SelectListItems so these can be rendered on the page
tuple.Item1.Options = GetSelectListItems(options);
return View(tuple);
}
[HttpPost]
public ActionResult SubmitOptions(IndexModel stuff)
{
double vehicleAmount = stuff.VehiclePrice;
double depositAmount = stuff.DepositAmount;
DateTime deliveryDate = stuff.DeliveryDate;
string financeOption = stuff.FinanceOption;
int finOption = Convert.ToInt32(stuff.FinanceOption) * 12;
return View("About");
}
并查看 -
@using LoanRepaymentSystem.Models
@model Tuple<IndexModel, DateTime>
@using (Html.BeginForm("SubmitOptions", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<div class="row">
<div class="form-group">
@Html.LabelFor(m => m.Item1.VehiclePrice)
@Html.TextBoxFor(m => m.Item1.VehiclePrice, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Item1.DepositAmount)
@Html.TextBoxFor(m => m.Item1.DepositAmount, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Item1.DeliveryDate)
@Html.TextBoxFor(m => m.Item1.DeliveryDate, String.Format("{0:d}", Model.Item2.ToShortDateString()), new { @class = "datefield, form-control", type = "date" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Item1.Options)
@Html.DropDownListFor(m => m.Item1.Options, Model.Item1.Options, "Please select a finance option ...", new { @class = "form-control" })
</div>
<input type="submit" value="Submit Options" />
</div>
}
当我在所有字段中放置值并按下提交按钮代码
[HttpPost]
public ActionResult SubmitOptions(IndexModel stuff)
在控制器中运行。但是,IndexModel填充值不是输入的值,它们是零和空值。
你能看出我做错了吗?
请帮忙。
千电子伏