mvc post and get

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
I have simple mvc project with c#. and I only want to post <input type="text" id="email" name="email">
To controller. Assume the view name is "Quote"

and I want to use same controller name - one for get, and one for post.

Can you show me in codes how it works? I am new in .net mvc.

Thanks,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Have you checked the official MVC site? It has many tutorials, both in video format as well as text.

Author

Commented:
Yes I did. No one shown me how to have same controller name with different method http
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You can use an attribute for the different verbs:

[HttpGet]
public ActionResult Show(...)
{
   ...
}

[HttpPost]
public ActionResult Show(...)
{
   ...
}

Open in new window

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Author

Commented:
I try it and red line show up
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Where are you getting a red line? Show your code!
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
GET may or may not have parameters. By convention, GET parameters are passed via the URL.
POST may or may not have parameter. By spec, POST parameters are passed via the body. (I believe nothing in the spec forbids URL parameters.)

In MVC, however you send parameters they come into the actions as parameters--regular old C# parameters. GETs are usually simple types; POSTs are usually complex types (i.e. classes). Keep in mind that when you're inside your action, that is C# code. As such, you have to follow C# rules. If you have two actions (i.e. methods) with the same name, then the parameter list has to vary--either in number of parameters or in data types (or both). Simply attributing two actions with HttpGet or HttpPost is not enough to let the compiler differentiate between which action to invoke. Your bound to C# rules once you're working inside the controller.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial