简体   繁体   中英

How to update a record using MVC3 WebApi and AngularJs . Getting this error : Non-static method requires a target.<

I am working on MVC 3 project and i just need to add a new feature and to do so i'm using angularjs and webApi. to achieve this i'm doing the following below:

so i have a service function in angular side of things like this:

 function Send(params) {
            var request = $http.post('/api/updatelostorder/update/', params);
            return request;
        }

Params is an object like this

params = {QouteLineId: 1, LostReasonId: 5}

Server (C#) side of things:

[AcceptVerbs("GET", "POST")]
        [HttpPost]
        public  bool Update(QuotesTable model)
        {
            try
            {
                var t = db.QuoteLines.Single(x => x.QuoteLineID == model.QuoteLineID);
                if (ModelState.IsValid)
                {
                    model.LostReasonId = model.LostReasonId;
                }
            }
            catch (Exception)
            {

                throw;
            }
            db.Entry(model).State = EntityState.Modified;
            db.SaveChanges();
            return true;

        }

Now i'm not sure to what i'm doing wrong here but when i run my project and try to update a record i get the following error:

Non-static method requires a target.<

Does anyone know what this means and also does anyone know a better way to achieve this?

Thank you

Well, there is several wrong pieces in your code. I'll try to enumerate them, but I advise you to study a little more on how MVC and Entity Framework works in general:

1 - [AcceptVerbs("GET", "POST")]

Is there a reason why you marked your POST method also accept a GET? This really doesn't make any sense. If it is a POST, then, only [HttpPost] should be used.

2 - var t = db.QuoteLines ...

You are querying the database (I'm assuming db here is a instance of your EF context class), storing the possible result into t but never actually use t to update its values.

3 - if (ModelState.IsValid)

What is the purpose of this if statement? model.LostReasonId = model.LostReasonId;

4 - db.SaveChanges

Since you already queried the database and stored the entity in the t variable, you don't need to attach anything in the context. All the updates should go to the t variable.

Also, you need to move this code inside the try/catch block and not outside, since this could also throw exceptions.

This is how it should look like after some refactoring:

[HttpPost]
public  bool Update(QuotesTable model)
{
    try
    {
        var t = db.QuoteLines.SingleOrDefault(x => x.QuoteLineID == model.QuoteLineID);     

        if (!ModelState.IsValid)
        {
            //Throw some exception or any other validation logic
            //only if model is not valid.
        }

        if(t == null)
        {
            //Throw some exception or any other validation logic
            //only if the QuoteLine doesn't exist
        }       

        //I believe you meant something like this?
        t.LostReasonId = model.LostReasonId;

        //More updates here in t...

        db.SaveChanges();
    }
    catch (Exception)
    {
        throw;
    }

    return true;
}

Try this and see if the error still occurs. It should work now.

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