0

I am new to MVC so please be gentle.

I have model -

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

I have a controller -

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

and view -

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

When I put values in all the fields and press the submit button the code

 [HttpPost]
 public ActionResult SubmitOptions(IndexModel stuff)

in the controller runs. However the IndexModel stuff values are not the ones that were entered, they are zeros and nulls.

Can you see what I am doing wrong?

Please help.

Kev

5
  • You cannot use a Tuple to generate form controls - look at the name attributes your generating - they have no relationship to your IndexModel. You need to pass just an instance if IndexModel to the view which will have @model IndexModel Commented Feb 26, 2018 at 23:37
  • Possible duplicate of Razor form asp.net mvc Commented Feb 26, 2018 at 23:41
  • And I have no idea what you think Model.Item2.ToShortDateString() in your TextBoxFor() is doing (it is adding a html attribute) Commented Feb 26, 2018 at 23:43
  • And your DropDownListFor(m => m.Options, ..) makes no sense either - you cannot bind a <select> to a collection of complex objects. Best guess is that your trying to bind the selected option to your FinanceOption property?) Commented Feb 26, 2018 at 23:45
  • Thanks Stephen, that was help. Commented Feb 26, 2018 at 23:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.