Vetrivel mp
Vetrivel mp

Reputation: 1224

Maintaining state in MVC 3

I am simply devloping entry form using mvc 3. I have many text boxes,radio button, lables. so while post back data from view to controller it loses entered data. how to retain this for all postbacks? is there any mvc mechanisam avialable by default? which means by enabling some properties do we command mvc to handle state by itself?

i am not interested to send viewdata object to view. except that what is default mechanism availble in mvc3?

Update:

@using (Html.BeginForm())
{
<table>
    <tr>
        <td>

            @Html.TextBox("txtTitle")
@Html.CheckBox("chkOption")
        </td>
        <td>               
            @Html.TextBox("txtDetails")
@Html.DropDownList("drpList")
        </td>
        <td>
            <input type="submit" value="Cancel" name="action" />
            <input type="submit" value="Create" name="action" />

        </td>
    </tr>
</table>
}

How to maintain state for entered and selected value for all post backs?

Upvotes: 0

Views: 3828

Answers (2)

Adrian Thompson Phillips
Adrian Thompson Phillips

Reputation: 7141

It looks like you need to create a Model and use the @Html.TextBoxFor(), @Html.CheckBoxFor() and @Html.DropdownListFor() methods instead, on the properties of your model.

You would then have a similarly named action method, decorated with a [HttpPost] attribute and the model as a parameter. You will then be able to perform validation on attempted form submissions and process the model if the form state is valid.

I'd look into researching strongly typed views, models and maybe go through a few online tutorials.

The link included below seems a good tutorial, it's a few pages long, but should go through everything you need to get started.

http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

You could use Session to persist state across multiple postbacks.

Upvotes: 2

Related Questions