动态添加的强类型部分视图看不到模型

发布于 2024-09-27 12:29:41 字数 1098 浏览 2 评论 0原文

这是我的场景:我有一个带有下拉列表的视图,当用户选择列表中的项目时,我想动态插入一个部分视图,该视图是强类型的,具有与父视图相同的模型。我认为这是一个相当常见的场景,而且很容易做到,但我可能错了(或者只是缺乏经验)。

我尝试的方法是使用此脚本(jQuery),该脚本在列表“onchange”事件上触发:

function loadPartial() {
    var id = $("#MyClass_MyClassId").val();
    var prop = $("#MyClass_MyProperty").val();
    $("#partials").load('/MyController/LoadPartial/?val=' + prop);
}

此脚本调用一个控制器操作,该操作返回适当的部分视图并将其插入给定的 div(id=partials)。

问题:动态插入的部分视图中的模型似乎是空的。

这是我要插入的强类型部分视图之一(部分视图使用的模型与父视图模型相同):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.Controllers.MyViewModel>" %>

<div class="editor-label">
<label>Enter Data*</label>
</div>

<div class="editor-field">
    <%=Html.TextAreaFor(model => model.MyClass.MyProperty)%>
    <%=Html.ValidationMessageFor(model => model.MyClass.MyProperty)%>
</div>

我不确定这种方法是否是最好的,或者是否有更好的方法继续吧(比如使用 RenderPartial)。我不想在控制器操作中重新生成模型,因为我将丢失任何验证消息。我确实对符合我的要求的解决方案进行了相当彻底的搜索,但我无法找到一个,因此任何建议/建议将不胜感激。

Here's my scenario: I have a view with a dropdown list and when the user selects an item in the list I want to dynamically insert a partial view which is strongly typed with the same Model as the parent view. I thought this was a fairly common scenario and would be pretty easy to do but I may have been mistaken (or just inexperienced).

The approach I tried was to use this script (jQuery) which gets fired on the lists 'onchange' event:

function loadPartial() {
    var id = $("#MyClass_MyClassId").val();
    var prop = $("#MyClass_MyProperty").val();
    $("#partials").load('/MyController/LoadPartial/?val=' + prop);
}

This script calls a controller action which returns the appropriate partial view and inserts it into the given div (id = partials).

Problem: The Model in the dynamically inserted partial views appears to be empty.

Here is one of the strongly typed partial views I'm inserting (the model used by the partial views is the same as the parent views model):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.Controllers.MyViewModel>" %>

<div class="editor-label">
<label>Enter Data*</label>
</div>

<div class="editor-field">
    <%=Html.TextAreaFor(model => model.MyClass.MyProperty)%>
    <%=Html.ValidationMessageFor(model => model.MyClass.MyProperty)%>
</div>

I'm not sure if this approach is the best or if there is a better way to go about it (like using RenderPartial). I do not want to regenerate the Model in the controller action because I will lose any validation messages. I did do a fairly thorough search for a solution that matched my requirements but I was not able to find one so any recommendations/suggestions would be greatly appreciated.

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

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

发布评论

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

评论(1

筱果果 2024-10-04 12:29:41

你说这是有效还是无效?你的问题令人困惑。

这是我将使用的技术,所以我不确定问题所在。

Are you saying this is working or this is not working? Your question is confusing.

This is the technique I would use so I'm not sure that the problem is.

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