Understanding [HttpPost], [HttpGet] and Complex Actionmethod parameters in MVC

后端 未结 3 1938
悲&欢浪女
悲&欢浪女 2020-12-30 05:50

I am very very new to MVC the design-pattern and also the Framework. I am also not extremely well- versed in fundamentals of ASP.NET Forms. However, I do understand the basi

3条回答
  •  执念已碎
    2020-12-30 06:19

    The [HttpPost] attribute tells the routing engine to send any POST requests to that action method to the one method over the other. This is a type of overloading.

    Why is this second method adorned with [HttpPost] when the first method does not require any attributes?

    The default for a method is [HttpGet]. Because of that, no attribute is needed.

    Are there any guidelines on when to use [Http] attributes and when not?

    Ideally, attributes should be on every method, in order to avoid confusion. As you get more familiar with how things are working, you will often take shortcuts (as with everything else), and omit them when you know that they are not necessary.

    Since MyViewModel is a complex type, you can not pass it as part of URL. How can you call it?

    The data will be turned into the model from the data in the body of the request. This can come either as a JSON object, or as Form data. (There are tricks to get the object initialized from the URL, but they can be a little complicated and advanced.)

提交回复
热议问题