如何阻止ASP.NET MVC在View中缓存无效数据?

时间:2022-11-04 04:14:52

I have a situation that I can't seem to find any help on. I looked through many questions on here, but can't seem to find anybody that has asked (or answered) my specific question. Here it is:

我有一种情况似乎无法找到任何帮助。我在这里查看了很多问题,但似乎找不到任何提出(或回答)我的具体问题的人。这里是:

Assume I have 2 categories:

假设我有两个类别:

  • Paper (id: 1)
  • 纸(id:1)

  • Plastic (id: 2)
  • 塑料(id:2)

The user clicks on Paper, change the name to Plastic, and click 'Submit'.

用户单击Paper,将名称更改为Plastic,然后单击“Submit”。

Inside my controller I discover that there is a already a category named Plastic so I re-sends the page to the user with a friendly message.

在我的控制器内部,我发现已经存在一个名为Plastic的类别,因此我将页面重新发送给用户并发送友好消息。

The problem arises when the user decides to not change the value, and instead navigate away from the page. They do some other things and then click on Paper again to change it's name. Only this time they actually see the name Plastic in the textbox!?

当用户决定不更改值,而是离开页面时,会出现问题。他们做了其他一些事情,然后再次点击纸张更改它的名称。只有这次他们才真正在文本框中看到塑料这个名字!?

I have this problem on all of my controllers when I reject invalid form data. How do I fix this problem?

当我拒绝无效的表单数据时,我的所有控制器都出现此问题。我该如何解决这个问题?

1 个解决方案

#1


1  

I just wanted to answer this question myself to indicate my question was answered by queen3 in a comment. I discovered that the value was coming back from my LinqToSql with the invalid value.

我只是想自己回答这个问题,以表明我的问题在评论中由queen3回答。我发现该值是从我的LinqToSql返回的,其值无效。

The way that I fixed the issue was by registering the Castle Windsor PerWebRequestLifestyleModule http module like so:

解决问题的方法是注册Castle Windsor PerWebRequestLifestyleModule http模块,如下所示:

<httpModule>
 <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel" />
 ...
</httpModule>

Then I went to each component and set the lifestyle to PerWebRequest like so:

然后我去了每个组件并将生活方式设置为PerWebRequest,如下所示:

<component 
    id="..."
    service="..."
    type="..."
    lifestyle="PerWebRequest">

    ...

  </component>

Thanks queen3!

#1


1  

I just wanted to answer this question myself to indicate my question was answered by queen3 in a comment. I discovered that the value was coming back from my LinqToSql with the invalid value.

我只是想自己回答这个问题,以表明我的问题在评论中由queen3回答。我发现该值是从我的LinqToSql返回的,其值无效。

The way that I fixed the issue was by registering the Castle Windsor PerWebRequestLifestyleModule http module like so:

解决问题的方法是注册Castle Windsor PerWebRequestLifestyleModule http模块,如下所示:

<httpModule>
 <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel" />
 ...
</httpModule>

Then I went to each component and set the lifestyle to PerWebRequest like so:

然后我去了每个组件并将生活方式设置为PerWebRequest,如下所示:

<component 
    id="..."
    service="..."
    type="..."
    lifestyle="PerWebRequest">

    ...

  </component>

Thanks queen3!