Why the postman is returning a "Non-static method requires a target."

.net-4.6 asp.net-mvc-5.2 c# entity-framework-6 linq

Question

I have a Save method in my SavesController (Web Api controller). When I tested it with postman it returned this.

    {
"message": "An error has occurred.",
"exceptionMessage": "Non-static method requires a target.",
"exceptionType": "System.Reflection.TargetException",
"stackTrace": "   at System.Web.Http.ApiController.    <InvokeActionWithExceptionFilters>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"
  }

My save method is this. Exception was thrown at if statement

[HttpPost]
public IHttpActionResult Save(SavesDto dto)
{
    var userId = User.Identity.GetUserId();

    if (_context.SavedPosts.Any(a => a.AttendeeId == userId && a.PostId == dto.PostId))
    return BadRequest("The save already exists.");

    var savedPost = new Saves
    {
        PostId = dto.PostId,
        AttendeeId = userId
    };
    _context.SavedPosts.Add(savedPost);
    _context.SaveChanges();

    return Ok();
}

I have searched through the internet.I found out that problem comes from using a null value within the LINQ. But Any method in Linq statement just returns true/false, why is it throwing this exception?

    public class SavesDto
{
    public int PostId { get; set; }
}

I called the Api here

@foreach(var post in Model)
{
<button
 data-post-id="@post.Id" 
class="btn btn-default btn-sm pull-right .js-toggle-save">Save ?</button
}

@section scripts
{
  <script>
$(document).ready(function()
{
    $(".js-toggle-save").click(function(e)
    {
        var button = $(e.target);
        $.post("/api/saves", { postId: button.attr("data-post-id") } )
        .done(function()
        {
            button.removeClass("btn-default")
            .addClass("btn-info").text("Saved");
        })
        .fail(function ()
        {
            alert("something failed");
        })
    });
}
);
</script>
}

PostId and AttendeeId are not nullable

public class Saves
{
    public ApplicationUser User { get; set; }
    public Post Post { get; set; }

    [Key]
    [Column(Order = 1)]
    public int PostId { get; set; }

    [Key]
    [Column(Order = 2)]
    public string AttendeeId { get; set; }

}
1
0
8/23/2016 12:49:10 AM

Popular Answer

This exception occurs when you use a variable in a lambda expression which is null. It appears that the dto object is null. So try doing this:

if (dto != null && _context.SavedPosts.Any(a => a.AttendeeId == userId
  && a.PostId == dto.PostId))
0
8/22/2016 6:42:13 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow