Friday, July 5, 2013

Httpget HttpPost in asp.net MVC Example | Sample asp.net MVC application using httpget and httppost

Httpget HttpPost in MVC

Using a simple controller and views i will show how to use HttpGet and HttpPost in asp.net MVC.

1. HomeController Code:

public class HomeController : Controller
    {
     
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Class1 cs)
        {
            return RedirectToAction("Home");
        }

        public ActionResult Home()
        {
            return View();
        }
    }

2. Razor View : Index.cshtml

On HttpGet this view will be called.
After we click on submit button the action under the HttpPost in the controller will be called which will redirect the action to Home().

  <div>
        @using (Html.BeginForm())
        {
            <div>
                <label>
                    Name</label></div>
            <div>
                <input type="text" id="txt1" /></div>
            <br />
            <div>
                <label>
                    Mobile</label></div>
            <div>
                <input type="text" id="txt2" />
            </div>
            <br />
     
            <input type="submit" value="Create" />
        }
    </div>


3. Razor View : Home.cshtml

On HttpPost this Home.cshtml view will be displayed.

  <div>
        Thanks for filling up the form.
  </div>


Result:

1. After we click on create the page is redirected to action mentioned under httppost

2.

No comments:

Post a Comment