Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In webforms, the first page load is distinguished by using IsPostback property and server controls are basically initialized with data or initial values in <strong>Page_Load</strong> event. These initial settings are persisted on subsequent post back events for the page. As you may already know, all this is accomplished by viewstate which is basically a hidden variable that contains state of all server controls on the page and gets posted back for each postback event. </p> <p>MVC doesn't have this mechanism and is completely stateless i.e, we need to manually assign values of controls for each request. For ex:</p> <p><strong>In Web forms</strong></p> <p>ASPX page:</p> <pre><code>&lt;asp:TextBox id="txtName" runat="server"/&gt; &lt;asp:Button id="btnSubmit" Text="submit" onclick = "btnSubmit_click" /&gt; </code></pre> <p>Code behind</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { if(!IsPostback) { txtName.Text = "test1"; } } protected void btnSubmit_click(object sender, EventArgs e) { //some code } </code></pre> <p>Enter a value and click on submit button, the value gets persisted event after postback due to viewstate.</p> <p><strong>In MVC</strong></p> <p>View</p> <pre><code>@using(Html.BeginForm()){ &lt;input id="Name" type="text" name="Name"/&gt; &lt;input type="submit" value="submit" /&gt; } </code></pre> <p>Controller</p> <pre><code>[HttpPost] public ActionResult Index(FormCollection values) { return View(); } </code></pre> <p>Enter a value and click on submit button, the value is not persisted across postback since there is no such persistence mechanism. So we need to achieve it manually</p> <p><strong>View</strong></p> <pre><code>@using(Html.BeginForm()){ &lt;input id="Name" type="text" name="Name" value="@ViewBag.Name"/&gt; &lt;input type="submit" value="submit" /&gt; } </code></pre> <p><strong>Controller</strong></p> <pre><code>[HttpPost] public ActionResult Index(FormCollection values) { ViewBag.Name = values["name"]; return View(); } </code></pre> <p>Note: I have used ViewBag only to show an example, ideally we need to pass viewmodel from controller.</p> <p><strong>Additional Inputs</strong></p> <p>Apart from its role of persisting state, ViewState can also be used to store values, say for ex: ViewState["test"] = objectoranyothervalue. In MVC, there are other techniques for state management like ViewBag, ViewData and TempData but difference is they don't get posted back to server like ViewState. They are used to just pass data from controller to view. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload