I have the following javascript and input fields within my model.
script type="text/javascript">
function sum() {
var txtFirstNumberValue = document.getElementById('money1').value;
var txtSecondNumberValue = document.getElementById('money2').value;
var txtThirdNumberValue = document.getElementById('money3').value;
var result = parseFloat(txtFirstNumberValue) + parseFloat(txtSecondNumberValue) + parseFloat(txtThirdNumberValue);
if (!isNaN(result)) {
document.getElementById('Total').value = result;
}
}
</script>
<div class="form-group">
@Html.LabelFor(model => model.Total, "Total", new { @class = "control-label col-md-5" })
<div class="col-md-offset-0">
<input type="text" id="Total" name="Total" />
@*@Html.EditorFor(model => model.Total)*@
@Html.ValidationMessageFor(model => model.Total)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.money1, "money1", new { @class = "control-label col-md-5" })
<div class="col-md-offset-0">
@*@Html.EditorFor(model => model.money1)*@
<input type="text" id="money1" onkeyup="sum();" name="money1" />
@Html.ValidationMessageFor(model => model.money1)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.money2, "money2", new { @class = "control-label col-md-5" })
<div class="col-md-offset-0">
@*@Html.EditorFor(model => model.money2)*@
<input type="text" id="money2" onkeyup="sum();" name="money2" />
@Html.ValidationMessageFor(model => model.money2)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.money3, "money3", new { @class = "control-label col-md-5" })
<div class="col-md-offset-0">
@*@Html.EditorFor(model => model.money3)*@
<input type="text" id="money3" onkeyup="sum();" name="money3" />
@Html.ValidationMessageFor(model => model.money3)
</div>
</div>
This all works fine. When I type a value into the 3 money fields the resulting value appears in the Total field. Within MVC if I click on the details view it shows all four values, however if I click on the edit view all four fields are blank. Question is how do I get the values to appear and remain in edit mode?