如何在部分视图中保留类型化视图模型对象的状态?
嗨,
我在 ASP.NET MVC 中有一个强类型视图。为了在未连接到字段(例如 TextBoxFor)时跟踪模型的属性,我将不得不使用 HTML.HiddenFor 元素。这使得操作数据并将其返回到服务器变得容易。
现在假设我的强类型视图包含一个部分视图,其中我从主视图中提供了一些模型,如下所示:
<% Html.RenderPartial("~/Views/Ad/Partial/ListSettings.ascx", Model.ALS); %>
所有用于读取和操作 ALS proerpty(复杂对象)的 javascript 都放置在部分视图中以进行 abel在其他主视图上使用局部视图。
在这个部分视图中,我还有以下几行:
<%: Html.HiddenFor(c => c.CP) %>
<%: Html.HiddenFor(c => c.L) %>
<%: Html.HiddenFor(c => c.OB) %>
<%: Html.HiddenFor(c => c.ST)%>
<%: Html.HiddenFor(c => c.P)%>
这可以很好地由 javascript 读取,但是在提交视图时,这些属性的设置不会返回到服务?
我确实尝试将它们放在主视图中,如下所示:
<%: Html.HiddenFor(c => c.ALS.CP) %>
<%: Html.HiddenFor(c => c.ALS.L) %>
<%: Html.HiddenFor(c => c.ALS.OB) %>
<%: Html.HiddenFor(c => c.ALS.ST)%>
<%: Html.HiddenFor(c => c.ALS.P)%>
并将 javascript 指向正确的字段,但即使这有效,问题是部分视图不会包含它需要的所有内容,我将不得不在中实现这些隐藏的文件部分视图放置在每个主视图中?
这要怎么处理呢?
此致
Hi,
I have a Strongly typed view in ASP.NET MVC. To keep track on properties of the model when thay are not connected to a field(ex TextBoxFor) I will have to use a HTML.HiddenFor element. This makes it easy to manipulate the data and get it back to the server.
Say now that my strongly typed view contains a partial view where I provide a bit of my model from the main view, like this :
<% Html.RenderPartial("~/Views/Ad/Partial/ListSettings.ascx", Model.ALS); %>
All the javascript for reading and manipulate the ALS proerpty(complexed object) is placed in the partial view to be abel to use the partial view on other main views.
In this partial view I have also the following lines :
<%: Html.HiddenFor(c => c.CP) %>
<%: Html.HiddenFor(c => c.L) %>
<%: Html.HiddenFor(c => c.OB) %>
<%: Html.HiddenFor(c => c.ST)%>
<%: Html.HiddenFor(c => c.P)%>
And this works fine to read by the javascript but when submitting the view the settings of these properties will not follow back to the service?
I did try to place them in the main view instead like this :
<%: Html.HiddenFor(c => c.ALS.CP) %>
<%: Html.HiddenFor(c => c.ALS.L) %>
<%: Html.HiddenFor(c => c.ALS.OB) %>
<%: Html.HiddenFor(c => c.ALS.ST)%>
<%: Html.HiddenFor(c => c.ALS.P)%>
And point the javascript to the correct fields but even if this works the problem is that the partial view will not caontain everything it needs, I will have to implement these hidden filed in every main view that the partial view is placed in?
How to handle this?
BestRegards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果用户无法修改所有这些字段,您可以简单地包含一个包含一些 id 的隐藏字段,以便您可以从数据源中取回它们。例如:
现在,如果该 id 包含在表单中或者可以由 javascript 使用并通过 AJAX 请求发送,则该 id 将被发送回控制器。在控制器操作内部,您将使用 id 并从最初存储的位置获取所有内容:
如果属性是可修改的,那么您将使用标准输入元素,例如文本框和下拉列表,允许用户设置其值,然后设置您的 POST 控制器操作将以更新的视图模型作为参数。
If all those fields are not going to be modifiable by the user you could simply include a hidden field containing some id that will allow you to fetch them back from the data source. For example:
and now this id will be posted back to the controller if it is wrapped inside a form or could be used by javascript and sent along an AJAX request. Inside the controller action you would use the id and fetch everything from wherever it was initially stored:
If the properties are modifiable then you would use standard input elements such as textboxes and dropdownlists allowing for the user to set their values and then your POST controller action would take the updated view model as argument.