Yannis
Yannis

Reputation: 6157

ASP.NET MVC : Form values get cached after the first post

I might be doing something completely wrong here but for some reason the values of this form i have in one of my pages get cached after the first post. This is not a browser thing because even if i open a different browser the values posted are still cached.

My form is very simple:

<form action="/post/save" method="post">
    <label>Type here whatever you want, quick and without thinking</label>
    <%= Html.TextArea("Body", new { @class = "post", rows="3" })%>
    <input type="submit" value="Publish" class="big_button red" />
</form>

My controller action is even simpler:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save(ArticleView form)
    {
        Article article = _ArticleViewMapper.Map(form);

        article.UpdatedBy = "guest";
        article.CreatedBy = "guest";
        article.UpdatedOn = DateTime.Now;
        article.CreatedOn = DateTime.Now;

        CMSFactory.CMS.ArticleRepository.Save(article);

        return RedirectToAction("Index", "Home");
    }

The form object has only an ID and a Body property. Right at the top of the method, this object has the body of the previous (actually the first) post request. is there any caching going on that i am not familiar with? thx

EDIT:

I found that the origin of the problem is the way i register my controllers. In my application_start I have the following code:

        // This only initialized the Castle IOC container
        DependencyRegistrat.Init();

        DependencyRegistrat.GetDefaultContainer().RegisterAll<IController>(typeof(HomeController).Assembly, ComponentLifecycle.Transient);

        ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory(DependencyRegistrat.GetDefaultContainer()));

MyControllerFactory is as follows:

    protected override IController GetControllerInstance(Type controllerType)
    {
        Check.IsNotNull(controllerType, "The given controller type was null, but must be provided");

        return (IController) _Container.Resolve(controllerType);
    }

    public override void ReleaseController(IController controller) 
    {
        var disposable = controller as IDisposable;

        if (disposable != null) disposable.Dispose();

        _Container.Release(controller);
    }
}

Upvotes: 1

Views: 2572

Answers (3)

&#199;ağdaş Tekin
&#199;ağdaş Tekin

Reputation: 16651

Are you sure that's not the ModelState keeping the post data cached? Because that's where the HtmlHelpers (Html.TextArea() in this case) get their values from.

Try adding ModelState.Clear(); before redirecting in your Save action.

Or if you just want to clear that field, rather than the whole ModelState, then : ModelState["Body"].Value = new ValueProviderResult("", "", CultureInfo.CurrentCulture);

Upvotes: 4

Yannis
Yannis

Reputation: 6157

The problem was in the controller registration. I am using Castle's IOC container and for some reason,

this (which works):

_Container.AddComponentLifeStyle(typeof(K).FullName.ToLower(), typeof(K), (lifecycle);

is different that this (which doesnt work)

_Container.AddComponentLifeStyle<I, K>(lifecycle);

Upvotes: -1

Robert Harvey
Robert Harvey

Reputation: 180788

Are you adding a new article or updating an existing one?

If you are updating an existing article you should be retrieving the existing article from the repository first.

If you are creating a new article you should be newing up an Article object.

Upvotes: 0

Related Questions