Sunday, 18 November 2012

ASP.NET MVC Get and Post request?


Yes, In web forms first request is by default is get request then post request. generally we get the from then
fill the form ---> finally submit to server.

asp.net engine---> no need to specify the action method in <form> tag. on render server will send the method name and postback form name. ---> once see the view source. // implictly done by asp.net engine

<form id="form1" action="Default.aspx" method="post">

</form>
yes, but comming asp.net mvc we need to specify the action and method names. in Every Controller we have

2 methods with same name--->


public ActionResult Create()
        {

            return View();           // method="GET"
        }

How asp.net mvc know when to use which method. by default we don't specify an attribute like [httpPost] then which consider as Get Action so first executes ---> Create method.

then after filling the form submit to server---> so

<input type="submit" value="Save" />----->Behaviour is POST

SO below POST method will be executed.

  [HttpPost]                                       // method="POST"
        public ActionResult Create(Employee collection)
        {
            try
            {
             
                conobj.emps.Add(collection);
                conobj.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

as well at runtime above method is called like below:



How to make a view as Strongly --typed and how Model property works?

yes, when we creating view --as strongly types which generates  a code like


@model MvcthreeApplicationDemo.Models.Employee

here 
 Model represents current type(Employee) all properties belongs to Employee is accessable here.

in case of Create ---we creating single employee at a time so 

@model MvcthreeApplicationDemo.Models.Employee   // single employee only



In case of List ----we are returning multiple employes so


@model IEnumerable<MvcthreeApplicationDemo.Models.Employee>  // multiple employees




No comments:

Post a Comment