73017

Having trouble sending formatted TextBoxFor Value back to the controller

Question:

The following code works fine when I enter a number (e.g., 123456) without commas: the number is sent to the Save action of the controller and 123,456 is displayed in the textbox.

However, when when I click Save again, and try to enter the number 123,456 with the comma, the Save action only receives a zero in the model parameter and a zero is then displayed in the textbox.

What am I doing wrong?

Here is the model class:

public class FormatTextBoxFor { public double MyDouble { get; set; } }

Here is the controller:

public class FormatTextBoxForController : Controller { public ActionResult Index() { FormatTextBoxFor model = new FormatTextBoxFor(); return View(model); } [HttpPost] public ActionResult Save(FormatTextBoxFor model) { return View("Index", model); } }

And the view:

@using (Html.BeginForm("Save", "FormatTextBoxFor", FormMethod.Post)) {

@Html.TextBoxFor(x => x.MyDouble, string.Format("{0:n0}", Model.MyDouble) )

<input type="submit" name="submit" value="Save" id="submit" /> }

Answer1:

This happens because on the server 123,456 is treated as text and not a number as you defined it in your model. On the server any value sent will will be mapped to the property of you model and parsed using default parser. You have to reformat the value before sending it to the server.

Recommend

  • Reload MVC2 user control with jQuery
  • How does ASP.NET get line numbers in it's generic error handler
  • The ViewData item that has the key 'MaritalStatus' is of type 'System.String' bu
  • The model item passed into the dictionary is of type A, but this dictionary requires a model item of
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Python pickle not one-to-one: different pickles give same object
  • JqueryMobile Popup menu is not working
  • Breaking out column by groups in Pandas
  • Not able to aggregate on nested fields in elasticsearch
  • Unable to decode certificate at client new X509Certificate2()
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Use of this Javascript
  • How do I alternate colors in Flat List (React Native)
  • $wpdb not working in file of WordPress plugin
  • Meteor: Do Something On Email Verification Confirmation
  • Meteor helpers not available in Angular template
  • Linq Objects Group By & Sum
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Where to put my custom functions in Wordpress?
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator
  • Rearranging Cells in UITableView Bug & Saving Changes
  • RestKit - RKRequestDelegate does not exist
  • How to format a variable of double type
  • Cannot Parse HTML Data Using Android / JSOUP
  • Buffer size for converting unsigned long to string
  • WPF Applying a trigger on binding failure
  • Proper way to use connect-multiparty with express.js?
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • unknown Exception android
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?