Base solution for your next web application
Open Closed

Form Validation in Partial View using Ajax #1041


User avatar
0
tjackadams created

Hello,

i am currently trying to post a form in a partial view using abp.ajax. The actual form posting works fine, and when the form is filled out correct, the success event fires as expected. The problem arises when there is an error in the form, the InclusiveUnits field has a range applied to it (via Data Annotations). When a value is outside of this range the CheckModelState(); throws an exception as expected, however this doesn't get returned wrapped, it gets returned as html.

Here is the javascript

var form = $("#UpdateForm");

       abp.ajax({
                url: form.attr("action"),
                data: JSON.stringify({
                    accountId: $("#AccountId").val(),
                    accountName: $("#AccountName").val(),
                    contractName: $("#ContractName").val(),
                    serviceName: $("#ServiceName").val(),
                    repositoryId: $("RepositoryId").val(),
                    inclusiveUnits: $("#InclusiveUnits").val(),
                    autoTaskUnits: $("#AutoTaskUnits").val()
               
                })
            })
            .done(function(response) {
                abp.message.success("success");
            })
            .fail(function (response) {
                abp.ajax.showError(response);
                abp.ajax.logError(response);
            });
    });

When the call is successful i receive this response

{
  "success": true,
  "result": {
    "contractId": 39
  },
  "error": null,
  "unAuthorizedRequest": false
}

however when it fails i get a html page that starts with the following

Form is not valid. Please check and fix errors.

I have checked and i am running the latest version of ABP v8.4.0. Is there an issue with the way i am trying to display the validation errors or could this be a bug?

Any help is much appreciated. Tom


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    What is return type of your method? JsonResult?

  • User Avatar
    0
    tjackadams created

    Yes it is, here is Controller

    [HttpPost]
            public async Task<JsonResult> UpdateCloudRepositoryContract(SplaVeeamCloudRepositoryContractUpdateViewModel model)
            {
                CheckModelState();
    
                DoStuf();
    
                return Json(new {ContractId = model.Id});
            }
    

    Thanks

  • User Avatar
    0
    hikalkan created
    Support Team

    OK, can you create a Github issue for that: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/new">https://github.com/aspnetboilerplate/as ... issues/new</a> There may be a bug, which we can fix in the next version. Thanks.

  • User Avatar
    0
    tjackadams created

    Github issue raised

    <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/997">https://github.com/aspnetboilerplate/as ... issues/997</a>

    Many Thanks!