ASP.net MVC 无法访问 ajax 控制器函数中的数据
ASP.net MVC can't access ajax data in the controller function
我使用 x-editable 并尝试 post 与我的控制器约会
x-可编辑代码是
<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>
现在我尝试访问以下控制器中的 posted 数据
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
我收到以下错误
{"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."}
我知道控制器函数必须有 2 个参数而 ajax url 没有这些参数..但是当我将参数添加到 ajax url 时表示参数等同于静态值且无法更改
我该怎么办?
您需要为操作参数创建 class。 ASP.NET MVC doku 是这样说的:
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.
代码可能如下所示(我不是 VB 编码员):
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
希望对您有所帮助。
我使用 x-editable 并尝试 post 与我的控制器约会
x-可编辑代码是
<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>
现在我尝试访问以下控制器中的 posted 数据
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
我收到以下错误
{"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."}
我知道控制器函数必须有 2 个参数而 ajax url 没有这些参数..但是当我将参数添加到 ajax url 时表示参数等同于静态值且无法更改
我该怎么办?
您需要为操作参数创建 class。 ASP.NET MVC doku 是这样说的:
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.
代码可能如下所示(我不是 VB 编码员):
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
希望对您有所帮助。