MVC4 RC WebApi parameter binding

后端 未结 2 698
青春惊慌失措
青春惊慌失措 2021-01-04 14:36

I upgraded from MVC4 beta to RC and the latest autofac. The following action was binding properly, but now both parameters are null. I see they changed things about the Fo

2条回答
  •  半阙折子戏
    2021-01-04 14:49

    Not really sure why the change from Beta, but I was able to make it work by changing the action signature to:

    [HttpPost]    
    RedirectModel MyAction(MyActionDTO dto)
    

    and defining MyActionDTO as

     public class MyActionDTO 
     {
            public string value1 { get; set; }
            public string value2 { get; set; }
     }
    

    It was throwing an exception about not being able to bind to multiple body parameters using the two string paramaters. I guess using the DTO object more closely represents what you're sending in the AJAX call (a JSON object).

提交回复
热议问题