Devmonster
Devmonster

Reputation: 709

Postback Data is not retained

I just got out of ASP.NET Web Forms and is now an MVC newbie. I have read that MVC does not manage ViewState unlike ASP.NET Web Forms. I was wondering how you can achieve data persistence.

I have this simple form with a strongly-typed model:

class MyModel
{
    public string TextboxData { get; set; }
    public string HiddenData { get; set; }
}

With the following form:

@using (Html.BeginForm())
{
     @Html.TextBoxFor(m => m.TextboxData)
     @Html.HiddenFor(m => m.HiddenData)
}

My controller is a simple one. It simply processes the data in the model and passes it back to the view:

[HttpPost]
public ActionResult PerformAction(MyModel mv)
{
    mv.DoSomething();
    return View(mv);        
}

Since the controller just reuses the Model, am I correct to assume that all entered data via the previous submit will be retained on the next refresh?

Actually, the one for TextboxData was retained (after postback, the textbox still contains the entered data) but the one for HiddenData was not retained. Am I doing something wrong here?

Upvotes: 2

Views: 1482

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

If inside the mv.DoSomething(); you modify the value of either the TextboxData or HiddenData properties, those changes won't get reflected when you render the view. The reason for that is because HTML helpers such as TextBoxFor and HiddenFor are first looking inside the ModelState when binding and then in the model. This is by design.

So if you need to modify the originally posted values inside a POST action you need to remove them from the ModelState first:

[HttpPost]
public ActionResult PerformAction(MyModel mv)
{
    // we intend to modify the values => we must remove them from ModelState
    // or the HTML helpers will reuse the old values
    ModelState.Remove("TextboxData");
    ModelState.Remove("HiddenData");

    mv.TextboxData = "some new value";
    mv.HiddenData = "some new hidden value";

    return View(mv);        
}

Upvotes: 2

SBB
SBB

Reputation: 155

On a POST your model is automagically filled in.

See an explanation of how model binding works on this dotnetslackers article

Now to your problem - Looking at the code posted in the question, it looks like you are doing it right. To figure out the problem here is what you should do 1. View source of your page. 2. Check if the hidden field is rendered with a value (whatever value you are expecting)

If the hidden field rendered is not set with the value you are expecting to see, it probably means that on the get, when the model was created, "m.HiddenData" was not set with the proper values.

Hope this helps!!

Upvotes: 1

Shailesh
Shailesh

Reputation: 1218

Seems you are doing correct stuff, it should send hidden text data as well when posted back to server.

Seems Id for hidden field is duplicated or not valid. Please refer

Razor MVC model is losing data on Save action

ASP.Net MVC Hidden field not working as expected

Hope these posts may resolve your issue.

Upvotes: 2

Related Questions