3

Textbox value when submitting the form is null in the controller. Simplified structure of what I have is below:

MODEL

public class MyObject
{
    public decimal? DecimalProperty { get; set; }
}

VIEW

@model List<MyObject>

@for(var i = 0; i < Model.Count; i++) 
{
   using (Html.BeginForm("UpdateObject", "MyController", FormMethod.Post))
   {
      @Html.Textbox(String.Format("Model[{0}].DecimalProperty", i), Model[i].DecimalProperty)
      <input type="submit" value="Update"/>
   }
}

CONTROLLER

public ActionResult UpdateObject(MyObject myObject)
{
   // Do Stuff...
}

If I put a breakpoint in the controller method, then check the property values of myObject, the DecimalProperty is null. There are other properties on the actual object I'm using and those come across alright, but for some reason this property isn't. I haven't found anything that suggests that decimals must be handled differently than a DateTime or string. I have also tried writing out the html for the input by hand:

 <input type="text" name="@(String.Format("Model[{0}].DecimalProperty", i))" value="@Model[i].DecimalProperty" />

I have set the name and the id attributes of the textbox just to be on the safe side. Any ideas as to why my textbox value is null when I submit the form?

Setting the [HttpPost] attribute does not help.

10
  • 1
    You seen to have a list of models, why is that? If you have an enumerator on the view but only one object on the action, I'm not sure if the model binder will figure out what to do. Commented Aug 4, 2015 at 14:22
  • On the view my model is a list of objects. There are also multiple forms on the view (one for each object). Model[i] represents that specific element in the model. Commented Aug 4, 2015 at 14:26
  • Edited that into the view code block. Commented Aug 4, 2015 at 14:27
  • Try "decimal?" You have to account for the possible null value. Commented Aug 4, 2015 at 14:27
  • But if I put a value in the textbox, it shouldn't be null in the controller when I submit the form. But yes, the property is actually a decimal? I'll edit to reflect that. Commented Aug 4, 2015 at 14:30

1 Answer 1

2

The problem is in your action signature. You should change it to accept whole list of models, or change Html.TextBox's Name attribute equal to property name of MyObject, cause default MVC binder cannot map it correctly.

So, first option:

public ActionResult UpdateObject(List<MyObject> myObject)
{
   // Do Stuff...
}

Second option:

@Html.Textbox("DecimalProperty", Model[i].DecimalProperty)
Sign up to request clarification or add additional context in comments.

4 Comments

I actually JUST tried that (removing the Model[i] from the textbox name) and it worked. Awesome!
@Nick that's what I've been telling you :)
Yeah, I didn't understand what you were saying in the earlier comments, @AndreCalil. That was my failure.
@Nick or I wasn't explaining correctly :) anyway, glad it is working now

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.