MVC4 RC WebApi parameter binding

后端 未结 2 681
青春惊慌失措
青春惊慌失措 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).

    0 讨论(0)
  • 2021-01-04 14:59

    When you want to avoid using a DTO object, try this:

    [HttpPost]    
    RedirectModel MyAction(dynamic value1, dynamic value2) {
        string sValue1 = value1;
        string sValue2 = value2;
    
    0 讨论(0)
提交回复
热议问题