简体   繁体   中英

OnSuccess script inside my Ajax.BeginForm is passing wrong data

I am working on an as.net mvc web application. I have the following Ajax.BeginForm:-

@using (Ajax.BeginForm("AddServerToRack", "Server", new AjaxOptions

{
    InsertionMode = InsertionMode.InsertBefore,
    UpdateTargetId = "result",
    LoadingElementId = "progressdialog",
    HttpMethod= "POST"
    ,
    OnSuccess = "addserver",
    OnFailure = "createfail"




}))
{

which calls the following action method:-

  [HttpPost]
        [ValidateAntiForgeryToken]
        [CheckUserPermissions(Action = "Edit", Model = "Server")]
        [OutputCache(CacheProfile = "NoCache")]
        public ActionResult AddServerToRack(AddServerToRack s)
        {


            try
            {
                //code goes here
                repository.Save();

                return Json(new { IsSuccess = true, Update="",description = "Server Added Successfully.",rackid = s.rackID }, JsonRequestBehavior.AllowGet);

            }
            catch (DbUpdateConcurrencyException exception)
            {


                return Json(new { IsSuccess = false, description = "Record was Modified by another User." }, JsonRequestBehavior.AllowGet);




            }

            catch (DbUpdateException exception)
            {

                string error = String.IsNullOrEmpty(exception.InnerException.InnerException.Message) ? exception.InnerException.ToString() : exception.InnerException.InnerException.Message.ToString();
                return Json(new { IsSuccess = false, description = "Error occurred." + error }, JsonRequestBehavior.AllowGet);


            }
            catch (Exception e)
            {
                return Json(new { IsSuccess = false, description = "Error occurred." }, JsonRequestBehavior.AllowGet);
            }



        }

and the following OnSuccess script:-

function addserver(data) {
    if (data.IsSuccess = true) {

        $('#progress').show();
        $.ajax({
            url: '/Rack/RackServer',
            type: 'GET',
            data: {
                id: data.rackid
            },

            dataType:'html' ,
            //error: function (xhr) {
            //    alert('Error: ' + xhr.statusText);
            //},
            success: function (result) {
                $('#RackDetails').html(result);
                $('#progress').hide();
            },

        });

        jAlert(data.description, 'Creation Confirmation');
    }
    else if (data.IsSuccess = false)
    {
        jAlert(data.description,'Error');

    }
}

but the problem i am facing is that the the $.ajax({ call inside the script, will execute even if the data.IsSuccess value is false .So it seems that the script will not execution the if statement check and will always execute the Ajax call even if the IsSuccess does not equal true . so can anyone advice ?

Regards

Your if statement is wrong:

if (data.IsSuccess = true)

it should be:

if (data.IsSuccess == true)

You used "=" instead of "==" for comparing in you addserver() method

Use like below

if (data.IsSuccess == true)

or simple

    if (data.IsSuccess)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM