使用JavaScript

时间:2018-02-15 15:23:34

标签: javascript .net asp.net-mvc asp.net-ajax

我正在使用.NET MVC。我的问题是,如果JavaScript语句为真,我需要从调用中止AJAX。

<div class="Filter_inner_title white_unifrom out-of-grid">
            @using (Ajax.BeginForm("Search", "Protocol", new AjaxOptions
        {
            HttpMethod = "GET",
            UpdateTargetId = "ajax-content"
        }, new { id = "form" }))
            {
                <div class="head-sys-content dotted-gray-bg white-unifrom-small">
                    <ul class="table-head-branch">
                        <li>
                            <ul class="Select_box_white_bg">
                                <li>
                                    <label class="label">@Html.DrawLabel("BoxNr")</label>
                                    @Html.TextBoxFor(model => Model.Search.BoxNumber, new { @class = "text", @style = "width: 70px" })
                                </li>
                                <li id = "FromDateContainer">
                                    <label class="label">@Html.DrawLabel("from")</label>
                                    @Html.TextBoxFor(model => Model.Search.FromDate, new { @class = "text datePicker" })
                                    <div class="validation-protocol-message">
                                        @Html.ValidationMessageFor(model => Model.Search.FromDate)
                                    </div>                                    
                                </li>
                                <li>
                                    <label class="label">@Html.DrawLabel("to")</label>
                                    @Html.TextBoxFor(model => Model.Search.ToDate, new { @class = "text datePicker" })                                                                        
                                    @Html.ValidationMessageFor(model => Model.Search.ToDate)
                                </li>
                                <li>
                                    <div class="medium-chosen left">
                                        <label class="label">@Html.DrawLabel("protocolType")</label>
                                        @Html.DropDownListFor(model => Model.Search.ProtocolType, new SelectList(Model.ProtocolTypeList, "ID", "Label.Text", Model.Search.ProtocolType), Html.DrawLabel("all"), new { @class = "chosen-select" })
                                    </div>
                                </li>
                            </ul>
                            @Html.HiddenFor(model => Model.Search.PageIndex, new { @class = "PageIndex", @id = "PageIndex" })
                            @Html.HiddenFor(model => Model.Search.ContractId)
                            @Html.HiddenFor(model => Model.Search.IsFromContract)
                            <div class="big-button search left">
                                <input type="submit" value="@Html.DrawLabel("search")" id="SubmitButton" class="arrow-search" />
                            </div>
                        </li>
                    </ul>
                </div>     
            }

这是表格。

$("#form").on("submit", function (e) {
    debugger;

    var FromDate = $("#Search_FromDate").val();
    var ToDate = $("#Search_ToDate").val();
    var FromDateConvert = Globalize.parseDate(FromDate, datePattern);
    var ToDateConvert = Globalize.parseDate(ToDate, datePattern);
    var fromYear = FromDateConvert.getFullYear();
    var toYear = ToDateConvert.getFullYear();
    if (fromYear < 1970) {          
        $("#FromDateContainer span.field-validation-valid").html("@String.Format(Html.DrawLabel("DateOutOfBounds"),Model.Search.FromDate)");
        $("#FromDateContainer span.field-validation-valid").attr("class", "field-validation-error");
        //$(".field-validation-error").css("position", "absolute");
    }
});

如果if语句为true,我需要中止begin form调用。 有没有办法停止对该特定电话的每次通话?

1 个答案:

答案 0 :(得分:0)

我通过使用简单的from并在前验证之后进行ajax调用来解决这个问题

var datePattern ='@(Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern)';

$("#form").on("submit", function (e) {
    debugger;
    e.preventDefault();
    var FromDate = $("#Search_FromDate").val();
    var ToDate = $("#Search_ToDate").val();
    var FromDateConvert = Globalize.parseDate(FromDate, datePattern);
    var ToDateConvert = Globalize.parseDate(ToDate, datePattern);
    var fromYear = FromDateConvert.getFullYear();
    var toYear = ToDateConvert.getFullYear();

    if (fromYear < 1970 && toYear < 1970) {

        $("#ToDateContainer span.field-validation-error").html("@String.Format(Html.DrawLabel("DateOutOfBounds"),Model.Search.FromDate)");

        $(".field-validation-error").html("");
        $("#ToDateContainer span.field-validation-valid").addClass("field-validation-error");
        $("#FromDateContainer span.field-validation-valid").html("@String.Format(Html.DrawLabel("DateOutOfBounds"),Model.Search.FromDate)");
        $("#FromDateContainer span.field-validation-valid").attr("class", "field-validation-error");
        $("#ToDateContainer span.field-validation-valid").html("@String.Format(Html.DrawLabel("DateOutOfBounds"),Model.Search.FromDate)");
        $("#ToDateContainer span.field-validation-valid").attr("class", "field-validation-error");
        $(".field-validation-error").show();

    }
    else if (fromYear < 1970) {
        $(".field-validation-error").html("");
        $("#FromDateContainer span.field-validation-valid").html("@String.Format(Html.DrawLabel("DateOutOfBounds"),Model.Search.FromDate)");
        $("#FromDateContainer span.field-validation-valid").attr("class", "field-validation-error");
        $(".field-validation-error").show();
    }
    else if (toYear < 1970) {
        $(".field-validation-error").html("");
        $("#ToDateContainer span.field-validation-valid").html("@String.Format(Html.DrawLabel("DateOutOfBounds"),Model.Search.FromDate)");
        $("#ToDateContainer span.field-validation-valid").attr("class", "field-validation-error");
        $(".field-validation-error").show();
    }


    else {
        $(".field-validation-error").html("");
        $.ajax({
            url: '@Url.Action("Search", "Protocol")',
            contentType: "application/json; charset=utf-8",
            data: $(this).serialize(),
            success: function (data) {
                $('#ajax-content').html(data);
            }
        });

    }