JK.
JK.

Reputation: 5136

Cached partial view

I have this controller:

public ActionResult Save(Model m)
{
    var db = new Db();
    m.Prop = "New Value";
    db.Save(m);

    return PartialView("_ModelForm", m);
}

For some reason, although m is updated within the controller the "Old Value" of Prop is returned in the response rather than the "New Value".

I looked at the HTTP-response and the old value is returned as part of the response (the "New Value" change is ignored), so the problem is not that it is cached on the client.

I tried to decorate the controller with the OutputCache attribute but with no success.

db.Save has no side-effects that alters the Prop property.

Upvotes: 1

Views: 659

Answers (1)

Satpal
Satpal

Reputation: 133403

You have to use ModelState.Clear(). When your are reposting your View its value is populated from ModelState.

public ActionResult Save(Model m)
{
    ModelState.Clear();
    var db = new Db();
    m.Prop = "New Value";
    db.Save(m);

    return PartialView("_ModelForm", m);
}

Upvotes: 2

Related Questions