1

I've created a simple MVC application that takes information from a form and passes it to a controller

View:

@model MvcApplication1.Models.BetChargeModel


@using (Html.BeginForm())
{
<div>
    @Html.TextBoxFor(m=>m.numerators[0])   /   @Html.TextBoxFor(m=>m.denominators[0])
</div>

<div>
    @Html.TextBoxFor(m => m.numerators[1])   /   @Html.TextBoxFor(m => m.denominators[1])
</div>

<div>
    <input type="submit" value="Calculate" />
</div>
}

Controller:

 public ActionResult Index()
    {
        BetChargeModel model = new BetChargeModel();
        model.numerators = new List<double>();
        model.denominators = new List<double>();
        model.denominators.Add(1);
        model.denominators.Add(1);
        model.numerators.Add(0);
        model.numerators.Add(0);

        return View(model);
    }

[HttpPost]
    public ActionResult Index(BetChargeModel model)
    {
        double odds1 = model.numerators[0] / model.denominators[0];
        double odds = model.numerators[1] / model.denominators[1];

//other code
}

Model:

 public class BetChargeModel
{
    public List<double> numerators { get; set; }

    public List<double> denominators { get; set; }

    public double result { get; set; }

}

When I run this and try and post back information from the View the Model is coming back empty (full of null fields and zeros). Why is the data in my Textbox's not binding to the model?

(Edit: I've changed the model properties and reference to Numerators, Denominators and Result but haven't updated those here for sake of brevity)

Sperick
  • 2,691
  • 6
  • 30
  • 55

1 Answers1

4

Based on the names numerators and denominators it looks like you have implemented the lists as fields on the model.

You should use properties instead for the model binding to work properly (which I assume that @Raphael has done).

A working model would look like this:

public class BetChargeModel
{
    public List<double> numerators { get; set; }
    public List<double> denominators { get; set; }
}

... and to follow to naming conventions, make sure to rename numerators to Numerators and denominators to Denominators.

However, if this does not resolve your model binding issue, please elaborate and post your model implementation :)

-- UPDATE

As you have reported that the issue still persists I will post the code I have implemented based on your own provided samples - then you can cross check to make sure everything looks right on your machine - the code shown in the following is tested and works:

Model:

public class BetChargeModel
{
    public List<double> numerators { get; set; }
    public List<double> denominators { get; set; }
}

View:

@model Project.Models.BetChargeModel

@using (Html.BeginForm())
{
<div>
    @Html.TextBoxFor(m=>m.numerators[0])   /   @Html.TextBoxFor(m=>m.denominators[0])
</div>

<div>
    @Html.TextBoxFor(m => m.numerators[1])   /   @Html.TextBoxFor(m => m.denominators[1])
</div>

<div>
    <input type="submit" value="Calculate" />
</div>
}

Controller:

public ActionResult Index()
{
    var model = new BetChargeModel
                    {
                        numerators = new List<double> {0, 0},
                        denominators = new List<double> {1, 1}
                    };
    return View(model);
}

[HttpPost]
public ActionResult Index(BetChargeModel model)
{
    var odds1 = model.numerators[0] / model.denominators[0];
    var odds = model.numerators[1] / model.denominators[1];
    return null;
}
Community
  • 1
  • 1
Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
  • You assume well, I missed that possibility ! – Raphaël Althaus Oct 10 '12 at 20:47
  • @Lasse You're right. I did put fields instead of properties in my model. I have updated it now though and I'm still getting the original error so I've posted my model as you've requested – Sperick Oct 10 '12 at 20:50
  • @Sperick I have added my sample code now - I have tested it in an ASP.NET MVC4 application, and the model binding is working as expected :) – Lasse Christiansen Oct 10 '12 at 20:55
  • @Sperick And btw, after you did the model changes, did you remember to re-compile your solution? :) – Lasse Christiansen Oct 10 '12 at 20:58
  • @Lasse Yeah I recompiled. I've copied your controller code and used it instead of mine but still the same result. I'll elaborate. I get a 'ArgumentOutOfRange' exception on the first line in the HttpPost method. When I run the debugger I see that both the numerator and denominator have a count of zero. So the Lists both exist but the numbers that I inserted into the textboxes in my view aren't being entered into them. – Sperick Oct 10 '12 at 21:10
  • @Sperick have you tried to copy the view and model implementation as well to make sure we have 100% identic code? – Lasse Christiansen Oct 10 '12 at 21:17
  • @Lasse Just did it. I had an error in my view. Would you believe that I spotted it when I wrote it out in my original post and made a mental note to remove it in my program as well but forgot-hence why you were getting it to work and I wasn't. Thanks for your help! – Sperick Oct 10 '12 at 21:21