0

TextBoxFor calls my function but doesn't change my decimal display value (2.645844).

EditorFor sets my value to 1 decimal place (2.6), but doesn't call my function.

I have the following attribute in my model:

[DisplayFormat(DataFormatString = "{0:N1}", ApplyFormatInEditMode = true)]
public decimal AttritionRate { get; set; }

On a change event I need to run a recalculate function:

@for (int i = 0; i < Model.Positions.Count(); i++)
{
    @Html.TextBoxFor(p => p.Positions[i].AttritionRate, new { type = "number", onkeyup = "updateBigTotal(" + i + "," + Model.Positions[i].AttritionRate + ")" })
}

Where I then call the follwoing in javascript:

function updateBigTotal(arraypos, oldValue) {
    var newvalue = parseFloat($('#Positions_' + arraypos + '__AttritionRate').val());
   ... go on to perform calculations
}

How do I achieve both?

Janine
  • 61
  • 6
  • You have not shown your code for `EditorFor()` - to add attributes, refer [this answer](https://stackoverflow.com/questions/35206196/how-to-set-programatically-the-id-name-and-class-atribute-of-a-html-editfor-or/35248765#35248765) for an example. but I recommend stop polluting your markup with behavior and use [Unobtrusive Javascript](https://en.wikipedia.org/wiki/Unobtrusive_JavaScript) –  Jun 23 '17 at 12:57
  • EditorFor was exactly the same: @Html.EditorFor(p => p.Positions[i].AttritionRate, new { type = "number", onkeyup = "updateBigTotal(" + i + "," + Model.Positions[i].AttritionRate + ")" }) – Janine Jun 26 '17 at 12:58
  • Did you even bother to read the link I gave you - the format is `@Html.EditorFor(p => p.Positions[i].AttritionRate, new { htmlAttributes = new { type = "number" } }) ` –  Jun 26 '17 at 21:01
  • Yes, I am trying to understand unobtrusive javascript, but have a deadline to meet, according to your link, my whole project has been done incorrectly :( I don't have time to fix it – Janine Jun 27 '17 at 09:04

1 Answers1

0

After days of struggling, this seems to work:

@Html.TextBoxFor(p => p.Positions[i].AttritionRate, "{0:n1}", new { type = "number", onkeyup = "updateBigTotal(" + i + "," + Model.Positions[i].AttritionRate + ")" })
Janine
  • 61
  • 6