对 ASP .NET MVC 如何基于视图模型和发布的表单值填充字段感到困惑

发布于 2024-10-31 08:21:19 字数 1231 浏览 0 评论 0原文

不确定这个问题是否已经被问过。

我正在开发一个 ASP .NET MVC 2 网站。我的视图有一个下拉列表,用户可以在其中选择要显示的记录以进行编辑。更改此下拉列表时,当前所选记录的字段值会自动发布到服务器并保存在会话中。在响应中,服务器返回一个用于编辑新选择的记录的视图。

我遇到的问题是,显示新记录时会记住上一条记录中的某些字段值。

在我看来,我有类似以下内容导致了问题:

<%= Html.TextBox("ActionTypeDetails.DisplayName", 
                 Model.ActionTypeDetails.DisplayName) %>

这里我希望发布的数据填充一个名为 ActionTypeDetails 的参数,该参数具有多个属性,包括 DisplayName

我的控制器操作看起来像这样:

public ActionResult EditActionType(Guid PluginEditID, 
                                   Guid? SelectedActionTypeID, 
                                   ActionTypeViewObj ActionTypeDetails)

我尝试将我的视图更改为:

<%= Html.TextBox("PostedActionTypeDetails.DisplayName", 
                 Model.ActionTypeDetails.DisplayName) %>

我的控制器操作更改为:

public ActionResult EditActionType(Guid PluginEditID, 
                                   Guid? SelectedActionTypeID, 
                                   ActionTypeViewObj PostedActionTypeDetails)

但它仍然表现出相同的问题。有人可以解释一下为什么它会这样工作的逻辑吗?仍然允许我使用内置模型绑定的最简单的解决方法是什么?我知道我可以清除 ModelState,但这并不理想,因为我希望记住表单上的一些其他数据。

Not sure if this question has already been asked.

I am developing an ASP .NET MVC 2 site. My view has a drop down where user can select which record to display for editing. When this drop down is changed, the field values for the currently selected record automatically get posted to the server and are saved in the session. In the response the server then returns a view for editing the newly selected record.

The problem I am having is that some of the field values from the previous record are being remembered when the new record is displayed.

In my view I have something like the following which is causing the problem:

<%= Html.TextBox("ActionTypeDetails.DisplayName", 
                 Model.ActionTypeDetails.DisplayName) %>

Here I want the posted data to populate a parameter called ActionTypeDetails which has multiple properties including DisplayName.

My controller actions looks something like this:

public ActionResult EditActionType(Guid PluginEditID, 
                                   Guid? SelectedActionTypeID, 
                                   ActionTypeViewObj ActionTypeDetails)

I have tried changing my view to this:

<%= Html.TextBox("PostedActionTypeDetails.DisplayName", 
                 Model.ActionTypeDetails.DisplayName) %>

And my controller action to this:

public ActionResult EditActionType(Guid PluginEditID, 
                                   Guid? SelectedActionTypeID, 
                                   ActionTypeViewObj PostedActionTypeDetails)

Yet it still exhibits the same problem. Can someone please explain the logic to why it works like this? What is the easiest workaround that would still allow me to use the built in model binding? I know I can clear the ModelState but this is not ideal as I want some of the other data on the form to be remembered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2024-11-07 08:21:19

如果您回发到相同的 url,asp.net 具有保留字段值的机制。更多信息可以在此处找到。

If you post back to the same url, asp.net has mechanisms which preserve field values. More info can be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文