user2284341
user2284341

Reputation: 671

Html.TextBoxFor not submitting a value using HttpPost to controller

This is a simple issue and something I've done before but I can't see why this isn't working.

Here's my HTML helper:

<%= Html.TextBoxFor(am => Model.Amount, new { @id = "myAmount" })%>

I submit the form from the same view using a submit button:

<input id="myprocess" type="submit" value="Submit" name="myprocess">

And it hits my controller when I debug:

[HttpPost]
public ActionResult ProcessCreate(myModel m)
{..code here..}

But my model property 'm.Amount' is set to the default zero. I know this is obvious but I don't see why this isn't working.

Upvotes: 1

Views: 1871

Answers (4)

Luke
Luke

Reputation: 855

In my case, I had an @Html.HiddenFor referencing my property, somewhere at the top, by mistake. Removing this allowed for the value to be posted back.

Upvotes: 0

user2284341
user2284341

Reputation: 671

Thank you for your responses. I found what the problem was. There was some jquery which used a maskMoney function which changed the type from decimal to string. My model type for 'Amount' was decimal which had the default value 0. So I changed the type to string, passed it to my controller and parsed it back to a decimal type.

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239290

The lambda expression is incorrect. You need to change it to:

<%= Html.TextBoxFor(am => am.Amount, new { @id = "myAmount" })%>

Upvotes: 0

Matthew Jones
Matthew Jones

Reputation: 26190

Does your BeginForm call (or your <form>) wrap the textbox?

<% using (Html.BeginForm("ProcessCreate", "ControllerName", FormMethod.Post)) %>
<% { %>
    ....
    <%= Html.TextBoxFor(am => Model.Amount, new { @id = "myAmount" })%>
    ....
<% } %>

(This may be the wrong syntax, I haven't worked in MVC2 in a while).

Upvotes: 1

Related Questions