0

I have a form like this:

<div class="row">
    @using (Ajax.BeginForm("ImportFromExcel", "QuanLySim", null, new AjaxOptions
                    {
                        UpdateTargetId = "div-form-daily",
                        HttpMethod = "POST",
                        OnBegin = "QuanLyDaiLy.BeforeSend",
                        OnFailure = "QuanLyDaiLy.Error",
                        OnComplete = "QuanLyDaiLy.Success",
                        InsertionMode = InsertionMode.Replace
                    }, new { @class = "form-update", @role = "form", @id = "search-form" }))
    {
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <label for="so-ct" class="col-xs-12 col-sm-12 col-md-2 input-sm">File excel <span class="text-red required">*</span></label>
                    <div class="col-xs-12 col-sm-12 col-md-10 input-sm">
                        @Html.TextBoxFor(model => model.File, new { type = "file", accept = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel", @class = "btn btn-default btn-sm", style = "width: 100%;" })
                    </div>
                    <div class="col-xs-12 col-sm-12 col-md-2"></div>
                    <div class="col-xs-12 col-sm-12 col-md-10 color-red" style="padding-left: 10px; margin-top: 10px">
                        @Html.ValidationMessageFor(model => model.File)
                    </div>
                    <div class="clear"></div>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <label for="so-ct" class="col-xs-12 col-sm-12 col-md-2 input-sm">Giá nhân thêm</label>
                    <div class="col-xs-12 col-sm-12 col-md-10 input-sm">
                        @Html.TextBoxFor(model => model.Rate, new { @class = "form-control input-sm", placeholder = "Nhập tỷ giá cho giá sim..." })
                    </div>
                    <div class="col-xs-12 col-sm-12 col-md-2"></div>
                    <div class="col-xs-12 col-sm-12 col-md-10 color-red" style="padding-left: 10px; margin-top: 10px">
                        @Html.ValidationMessageFor(model => model.Rate)
                    </div>                     

                </div>
            </div>

            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <label for="so-ct" class="col-xs-12 col-sm-12 col-md-2 input-sm">Chọn đại lý <span class="text-red required">*</span></label>
                    <div class="col-xs-12 col-sm-12 col-md-10 input-sm">
                        @Html.DropDownListFor(model => model.AgentId, (IEnumerable<SelectListItem>)ViewBag.Agents, "Chọn...", new { @class = "form-control input-sm add-daily-sim filter-select" })
                    </div>
                    <div class="col-xs-12 col-sm-12 col-md-2"></div>
                    <div class="col-xs-12 col-sm-12 col-md-10 color-red" style="padding-left: 10px; margin-top: 10px">
                        @Html.ValidationMessageFor(model => model.AgentId)
                    </div>
                    <div class="clear"></div>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <label for="so-ct" class="col-xs-12 col-sm-12 col-md-2 ">Đảm bảo đã đúng giá?</label>
                    <div class="col-xs-12 col-sm-12 col-md-1">
                        @Html.CheckBoxFor(model => model.IsSure.HasValue)
                    </div>
                    <label for="so-ct" class="col-xs-12 col-sm-12 col-md-2 ">Xóa sim đại lý</label>
                    <div class="col-xs-12 col-sm-12 col-md-1 ">
                        @Html.CheckBoxFor(model => model.IsXoaSimCu.HasValue)
                    </div>
                </div>
            </div>

            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <div class="form-group">
                    <button type="button" class="btn bg-maroon btn-cons btn-sm" id="btn-import-excel">
                        <i class="fa fa-floppy-o fa-lg"></i>&nbsp; Lưu
                    </button>
                    <button class="btn btn-default btn-sm" type="button" onclick="location.href='@Url.Action("Index", "QuanLySim")'"><i class="glyphicon glyphicon-refresh fa-lg"></i>&nbsp; Quay về</button>
                </div>
            </div>
        </div>
    }
</div>

And i call jquery to validate form:

 if (!$(form).valid()) return;

But function form.valid() is error and return message in console like this : VM40309:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0

Everybody can help me the problem?

Thanks all

Dan LE
  • 27
  • 1
  • 7
  • You are using some Unicode characters in your fields there, try preceding yours strings with 'u' to inform the parser this is expected e.g. `placeholder = u"Nhập tỷ giá cho giá sim..."` – Geek Josh Sep 03 '16 at 22:56
  • @Geek Josh : If I remove @Html.CheckBoxFor(model => model.IsSure.HasValue) and @Html.CheckBoxFor(model => model.IsXoaSimCu.HasValue), it have no error. And i think that Unicode characters is not a problem. – Dan LE Sep 03 '16 at 23:17
  • You cannot use `CheckBoxFor()` to bind to a `nullable bool` Change `IsSure` to `bool` or use `EditorFor()` (which will generate a dropdownlist with 3 values for `true`, `false` and `null`) –  Sep 03 '16 at 23:56
  • @Stephen Muecke: i was change datatype from nullable bool to bool but it also error. – Dan LE Sep 04 '16 at 00:06

2 Answers2

1

Form validation code is not working properly. please check and also use the unobtrusive ajax as reference on the page : jquery.unobtrusive-ajax.min.js

tika
  • 341
  • 2
  • 11
0

I think this is the same problem as https://stackoverflow.com/a/14822755/1808720 Problem lies in combination of jquery 1.9+ and old jquery.validate.unobtrusive.js.

Download latest version of jquery.validate.unobtrusive.js from https://github.com/aspnet/jquery-validation-unobtrusive/releases and it should be fine.

haldyr
  • 341
  • 1
  • 6