使用mvc中的javascript从值设置文本框值

时间:2015-04-20 12:39:19

标签: c# jquery asp.net-mvc-4

我有一个叫做Gelir'的模型。当我更改文本框值并单击“创建”时提交按钮,文本框值为0.如果单击提交按钮,如何保护文本框值?

Gelir模型

public  class Gelir   
{
        [HiddenInput]
        public int Id { get; set; }


        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Messages))]
        [StringLength(100, ErrorMessageResourceName = "StringLength", ErrorMessageResourceType = typeof(Messages))]
        [Display(Name = "IsAd", ResourceType = typeof(Messages))]
        public string IsAd { get; set; }

        [DataType(DataType.Date)]
        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Messages))]
        [Display(Name = "Tarih", ResourceType = typeof(Messages))]
        public DateTime Tarih { get; set; }


        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Messages))]
        [Display(Name = "Tutar", ResourceType = typeof(Messages))]
        public decimal Tutar { get; set; }

        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Messages))]
        [Display(Name = "KDV18", ResourceType = typeof(Messages))]
        public decimal KDV18 { get; set; }

        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Messages))]
        [Display(Name = "KDV8", ResourceType = typeof(Messages))]
        public decimal KDV8 { get; set; }

        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Messages))]
        [Display(Name = "KDV1", ResourceType = typeof(Messages))]
        public decimal KDV1 { get; set; }

        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Messages))]
        [Display(Name = "KDV0", ResourceType = typeof(Messages))]
        public decimal KDV0 { get; set; }
}

创建视图

@using (Html.BeginForm("Create", "Gelir", FormMethod.Post, new { @class = "form-horizontal" }))
{ 
    <div class="editor-field">

        @Html.EditorFor(model => model.IsAd)

    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Tarih)

    </div>

    @*<div class="editor-field">
        @Html.EditorFor(model => model.Tutar)

    </div>*@
    <div class="control-group">
        <label class="control-label" for="KDV18">Tutar</label>
        <div class="controls">
            @Html.TextBoxFor(model => model.Tutar)

            <label class="radio-inline">
                <input type="checkbox" id="remember" value="">KDV Dahil</label>
           <br />  @Html.ValidationMessageFor(model => model.Tutar)
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="KDV18">KDV Oran</label>
        <div class="controls" id="kdvOranLst">

            <label class="radio-inline">
                <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="0.18">%18
            </label>
            <label class="radio-inline">
                <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="0.08">
                %8
            </label>
            <label class="radio-inline">
                <input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="0.01">
                %1
            </label>
            <label class="radio-inline">
                <input type="radio" name="inlineRadioOptions" id="inlineRadio4" value="0">
                %0
            </label>
        </div>

    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.KDV18)

    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.KDV8)

    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.KDV1)

    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.KDV0)

    </div>       
}

脚本代码

<script>
    var kdv18 = document.getElementById("KDV18");
    var kdv8 = document.getElementById("KDV8");
    var kdv1 = document.getElementById("KDV1");
    var kdv0 = document.getElementById("KDV0");

    kdv18.disabled = true;
    kdv8.disabled = true;
    kdv1.disabled = true;
    kdv0.disabled = true;
    function clear()
    {
        kdv18.value = '0';
        kdv8.value = '0';
        kdv1.value = '0';
        kdv0.value = '0';
    }
    $('input[name=inlineRadioOptions]').on('change', function () {
       // clear();
        var x = $('input[name=inlineRadioOptions]:checked').val();//seçilen kdv oranı
        var Tutar = $("#Tutar").val();
        if (Tutar.indexOf(',') > -1)
            Tutar = Tutar.replace(',', '.');
        var TutarDeger = parseFloat(Tutar);

        if (document.getElementById('remember').checked) {
            alert("checked");
            if (x == "0.18")
                Kdv18(TutarDeger,1);
            else if (x == "0.08")
                Kdv8(TutarDeger,1);
            else if (x == "0.01")
                Kdv1(TutarDeger,1);
            else if (x == "0")
                Kdv0(TutarDeger,1);
        }
        else {
            alert("You didn't check it! Let me check it for you.");
            if (x == "0.18")
                Kdv18(TutarDeger, 0);
            else if (x == "0.08")
                Kdv8(TutarDeger, 0);
            else if (x == "0.01")
                Kdv1(TutarDeger, 0);
            else if (x == "0")
                Kdv0(TutarDeger, 0);
        }
        alert($('input[name=inlineRadioOptions]:checked').val());
    });


    function Kdv18(TutarDeger, dahilDurum) {
        var yuzde18 = 0;
        if (dahilDurum == "1") {
            var kdvOran = 1.18;
            var yeniTutar = TutarDeger / kdvOran;
            yuzde18 = TutarDeger - yeniTutar;
            $("#Tutar").val(yeniTutar.toString());
        }
        else {
             yuzde18 = (TutarDeger * (0.18));
        }
        yuzde18 = yuzde18.toString();
        if (yuzde18.indexOf('.') > -1)
            yuzde18 = yuzde18.replace('.', ',');
        $("#KDV18").val(yuzde18.toString());
        alert($("#KDV18").val());
        $("#KDV18").change();
    }
    function Kdv8(TutarDeger, dahilDurum) {
        var yuzde8 = 0;
        if (dahilDurum == "1") {
            var kdvOran = 1.08;
            var yeniTutar = TutarDeger / kdvOran;
            yuzde8 = TutarDeger - yeniTutar;
            $("#Tutar").val(yeniTutar.toString());
        }
        else {
            yuzde8 = (TutarDeger * (0.08));
        }

        yuzde8 = yuzde8.toString();
        if (yuzde8.indexOf('.') > -1)
            yuzde8 = yuzde8.replace('.', ',');
        $("#KDV8").val(yuzde8.toString());
    }
    function Kdv1(TutarDeger, dahilDurum) {
        var yuzde1 = 0;
        if (dahilDurum == "1") {
            var kdvOran = 1.01;
            var yeniTutar = TutarDeger / kdvOran;
            yuzde1 = TutarDeger - yeniTutar;
            $("#Tutar").val(yeniTutar.toString());
        }
        else {
            yuzde1 = (TutarDeger * (0.01));
        }
        yuzde1 = yuzde1.toString();
        if (yuzde1.indexOf('.') > -1)
            yuzde1 = yuzde1.replace('.', ',');
        $("#KDV1").val(yuzde1.toString());
    }
    function Kdv0(TutarDeger, dahilDurum) {
        var yuzde0 = 0;
        if (dahilDurum == "1") {
            var kdvOran = 1;
            var yeniTutar = TutarDeger / kdvOran;
            yuzde0 = TutarDeger - yeniTutar;
            $("#Tutar").val(yeniTutar.toString());
        }
        else {
            yuzde0 = (TutarDeger * (0));
        }
        if (yuzde0.indexOf('.') > -1)
            yuzde0 = yuzde0.replace('.', ',');
        $("#KDV0").val(yuzde0.toString());
    }

</script>

0 个答案:

没有答案