Getting the values of action parameters within an action filter

前端 未结 2 1590
渐次进展
渐次进展 2020-12-28 14:54

I have an action that I am POSTing to from jquery:

[HttpPost]
public void UpdateGroupName(int groupId, string name)
{
         


        
相关标签:
2条回答
  • 2020-12-28 15:40

    Try without the stringify. I guess MVC is understanding another way of binding besides the request parameter -> action parameter. I guess it's understanding the json posted. JQuery, if you pass just the data object (without stringify) will post each field as a request parameter (at least, I think so). It's easy to try :)

    0 讨论(0)
  • 2020-12-28 15:42

    The reason your code doesn't work is because you are sending your request as a JSON string. So there are no request parameters in the POST body and you cannot fetch them in the Request.Params.

    So instead of:

    filterContext.HttpContext.Request.Params["groupId"]
    

    use:

    filterContext.Controller.ValueProvider.GetValue("groupId").AttemptedValue
    

    This will query the value provider (in your case the JsonValueProvider) to obtain the corresponding value send by the client.

    0 讨论(0)
提交回复
热议问题