Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a MVC application in which i will call a controller using ajax. I have setup everything fine but when i invoke the ajax call, i get Internal Server Error 500 from F12(Google Chrome). I double checked my codes(javascript and C#) and seem nothing wrong. Below are my codes:

$.ajax({
    type: "POST",
    url: "/PrawnFarm/UpdatePrawnFarmDetail",
    data: {
        'pH': $ph,
        'salPerc': $salPerc,
        'salDeg': $salDeg,
        'doMG': $doMG,
        'doDeg': $doDeg,
        'water': $water,
        'rain': $rain,
        'pondId': $pondId,
    },
    success: function (data) {
    },
    fail: function (jqXHR, textStatus, errorThrown) {
    }
});


[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UpdatePrawnFarmDetail(decimal pH, decimal salPerc, decimal salDeg, decimal doMG, decimal doDeg, int water, int rain, long pondId)
{
    using (DB_TestEntities db = new DB_TestEntities())
    {
        PrawnFarmDetail prawnFarmDetail = new PrawnFarmDetail();

        prawnFarmDetail.PH = pH;
        prawnFarmDetail.SAL_Percentage = salPerc;
        prawnFarmDetail.SAL_Degree = salDeg;
        prawnFarmDetail.DO_Volume = doMG;
        prawnFarmDetail.DO_Degree = doDeg;
        prawnFarmDetail.Water_Volume = water;
        prawnFarmDetail.RAIN = rain;
        prawnFarmDetail.PondId = pondId;

        db.SaveChanges();
    }
    return View();
}


What I have tried:

1. Debug the javascript and all values are captured correctly.
Posted
Updated 30-Apr-18 4:54am
Comments
F-ES Sitecore 30-Apr-18 9:06am    
You need to do some more debugging. Use the browser tools to look at the response from the call and the error details are probably in there. As mentioned in Solution 1 your code is doing nothing, but that's not going to cause a 500 error. Maybe the default View can't be found? We can't debug your code for you, only you can do that.
Jamie888 2-May-18 22:40pm    
Thank you sir for your suggestion. Turns out my issue to be missing anti-forgery token in my ajax script.

I guess you missed to add the object to db context.
db.PrawnFarmDetail.Add(prawnFarmDetail) 

before saving..

And I observed that method name is update. are you trying to update the entity then, first fetch it from the database and udpate, in this case
prawnFarmDetail
is not a new object.

--RA
 
Share this answer
 
v2
Comments
Jamie888 2-May-18 22:40pm    
Thank you sir for your help. Turns out to be missing anti-forgery token in my ajax call.
Quote:
[ValidateAntiForgeryToken]

Your AJAX request does not include the anti-forgery token. As a result, the server will reject the request.
 
Share this answer
 
Comments
Jamie888 2-May-18 22:39pm    
Yes sir, seems like this is the case here. I have modified my ajax script and everything works fine now. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900