0

I am updating some features in a site which is made in asp.NET MVC. I am trying to show an alert when it successfully saves data or when it has some error while saving. My code is something like this:

---------Controller -----

 public JsonResult Insert(string post)
        {
            var posted = Functions.SharePost(post)
            if (posted == true)
            {
                return Json(new { message = "true" });
            }
            return Json(new { message = posted});
        }

---- View code --------

@using (Ajax.BeginForm("Insert", "WebsitePosting", new AjaxOptions { HttpMethod = "POST",  OnComplete = "CheckError(error);" }))
{
  @Html.TextArea("post", new { data_val_required = "The field is required.", data_val = "true", style = "width:800px" })
  <input type="submit" value="Save" class="k-button" />
}

----- Script ----

<script>
function CheckError(error)
{
   if(error == "true"){alert("success");}
   else{alert("faild");}
 }
</script>

Can anyone tell me what I am doing wrong? Thanks in advance.

7
  • can you please post your error? Commented May 11, 2015 at 7:59
  • In your java script code you are showing "success message" when error == true. Please verify this once. Commented May 11, 2015 at 8:00
  • error will be an object that either contains a property message or a property success. You need to check those properties Commented May 11, 2015 at 8:01
  • @jinesh jain Oh Sorry i write it by mistake , let me update the question ,, actually the controller is returning true Commented May 11, 2015 at 8:03
  • Change your code to this >> if(error.message == "your post has been shared"){alert("success");} Commented May 11, 2015 at 8:05

1 Answer 1

1

Update

With your new edits, it should be

if (error.message == "true") { alert('success'); }

End update

Error is an object. Use

if (error) {alert('error');}

or with jquery

if (!jQuery.isEmptyObject(error)) { alert('error'); }

Your script...

<script>
function CheckError(error)
{
   if(error){alert("success");}
   else{alert("faild");}
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Dear i try it but not working ,, if you want to ask any other thing i can tell you ..
updated my answer, use error.message now, did not see you changed your question code. You should not change your question code unless it was incorrect when you put it up there so people who come along later can see the original question in its entirety.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.