1

I have this code on my View :

<div class="editor-label">
    @Html.Label("Periode de Validation")
</div>
<div class="editor-field">
    Mois:
<input data-val="true" data-val-number="The field PART1_MOIS must be a number."
    id="PART1_MOIS" name="PART1_MOIS" style="width: 80px" type="text" value="" />
    Ans :
<input data-val="true" data-val-number="The field PART1_ANS must be a number."
    id="PART1_ANS" name="PART1_ANS" style="width: 80px" type="text" value="" />
    Total en mois :
<input data-val="true" data-val-number="The field PART1_fPeriodeValid must be a number."
        id="PART1_fPeriodeValid" name="PART1_fPeriodeValid" style="width: 80px" type="text" value="" />
    @Html.ValidationMessageFor(model => model.fPeriodeValid)
</div>

I want to convert period in year + month in month, so i have this script :

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <script>jQuery(function ($) {
            $(":text[id*=_MOIS],:text[id*=_ANS]").blur(function () {
                var MOIS = $(":text[id*=_MOIS]").val();
                var ANS = $(":text[id*=_ANS]").val();
                var total = MOIS / 1 + ANS * 12;
                $(":text[id*=fPeriodeValid]").val(total);
            });
        });</script>

but I don't know how to save this result (input button periodeValid) on my database ...

I have tried this :

Total en mois :

<input data-val="true" data-val-number="The field PART1_fPeriodeValid must be a number." id="PART1_fPeriodeValid" name="PART1_fPeriodeValid" style="width: 80px" type="text" value="@Model.fPeriodeValide" />

My model is like this : public int? fPeriodeValid { get; set; }

2
  • how does your controller look like? it it passing the value to the controller? Commented Jun 24, 2014 at 8:13
  • also instead of using <input> try using @Html.TextBoxFor(model => model.fPeriodeValid, new { @data-val="true", @data-val-number="" }) so its mapped with your model Commented Jun 24, 2014 at 8:17

1 Answer 1

2

Mvc modelbinder Use from input name to create your action model. if you have a model like:

public class myModel
{
public int? fPeriodeValid { get; set; }
}

You Must change PART1_fPeriodeValid to fPeriodeValid

<input  id="PART1_fPeriodeValid" name="fPeriodeValid" ... />
Or
@Html.EditorFor(model => model.fPeriodeValid)

And your Action Like this

 public ActionResult Create(myModel model){
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works with the first solution !!! So really thank you ! I have tried also with name="@Model.fPeriodValid" but I didn't think like this...

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.