Skip to main content
 首页 » 编程设计

c#之如何将 POST 参数转换为 Action 参数

2024年02月06日12freeliver54

我无法对 POST 参数进行排序。它是什么类型的?如何放置正确的“排序”参数?我尝试了对象、数组、具有 sender 属性的特定对象,但没有任何帮助。

帖子正文:

current=1&rowCount=10&sort[sender]=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed 

代码:

[HttpPost] 
public ActionResult OrganizationPosts( int current, int rowCount, string sort = null, string searchPhrase = null ) 
{            
    return Json(  
        new {  
            current = 1, 
            rowCount= 10, 
            rows = new[]{  
                new { id= 16, post = "post 16" }, 
                new { id= 117, post = "post 17"},  
                new { id= 19, post = "post 19"}  
            }, 
            total = 1123  
        }); 
    }  

请您参考如下方法:

模型联编程序不会将 sort[sender] 绑定(bind)到排序,如果您将请求更改为 sort=asc 它将起作用。

如果你也需要发件人,只需添加发件人,将其作为单独的参数添加,即:

current=1&rowCount=10&sort=asc&sender=someSender&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed 

并将方法签名更改为:

public ActionResult OrganizationPosts( int current, int rowCount, string sort = null, string sender = null, string searchPhrase = null) 

如果您无法更改请求,则必须创建一个自定义模型 Binder : 这是一个可以提供帮助的链接:http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder