Search code examples
jqueryasp.net-web-apiasp.net-ajaxx-editable

ASP.net MVC can't access ajax data in the controller function


I use x-editable and try to post date to my controller

x-editable code is

<span class="editable editable-click" title="" data-type="select" data-value="1" data-pk="2" data-url="http://localhost:58250/api/updateeffortitems/save" data-title="Work group" data-source="[{ value: 1, text: 'Project'},{ value: 2, text: 'Service'},{ value: 3, text: 'Process'},{ value: 4, text: 'Training'},{ value: 5, text: 'Others'}]" data-original-title="Project">Project</span>

now i try to access posted data in the following controller

Public Class UpdateEffortItemsController
    Inherits ApiController
    Public Function save(pk As Integer, value As String) As String

        Dim db As New RA_SQLEntities

        Dim row As Ra_activity_log
        row = db.Ra_activity_log.Where(Function(XX) XX.Activity_log_key = pk).SingleOrDefault()
        row.Comment = value
        db.SaveChanges()

        Return "done"

    End Function

End Class    

and i got the following error

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:58250/api/updateeffortitems/save'.","MessageDetail":"No action was found on the controller 'UpdateEffortItems' that matches the request."}

i know the controller function must have 2 params and the ajax url didnt have these params .. but when i add the params to ajax url that mean the params equel to static value and not able to change

what should i do?


Solution

  • You need to create a class for the action parameters. Here is what the ASP.NET MVC doku says:

    By default, Web API uses the following rules to bind parameters:

    • If the parameter is a “simple” type, Web API tries to get the value from the URI. Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string. (More about type converters later.)
    • For complex types, Web API tries to read the value from the message body, using a media-type formatter.

    Code might look like this (I'm not a VB coder):

    Public Class XEditablePost
        Public Property Pk() As Integer
        Public Property Value() As String
    End Class
    
    ....
    
    Public Function save(payload As XEditablePost) As String
        ....
    End Function
    

    Hope that helps.