如何在ASP中保存输入值.NET与剃刀

本文关键字:NET 剃刀 输入 保存 ASP | 更新日期: 2023-09-27 18:10:06

我在我的视图上有这样的代码:

<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>

我想把year + month中的周期转换成月,所以我有这个脚本:

<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>

但我不知道如何保存这个结果(输入按钮periodeValid)在我的数据库…

我已经试过了:

总长度:

<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" />

我的模型是这样的:public int? fPeriodeValid { get; set; }

如何在ASP中保存输入值.NET与剃刀

Mvc modelbinder使用from input name来创建你的动作模型。如果你有这样一个模型:

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

必须将PART1_fPeriodeValid更改为fPeriodeValid

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

And your Action Like this

 public ActionResult Create(myModel model){
}