开发者

Dynamically added, strongly typed partial views do not see the Model

开发者 https://www.devze.com 2023-01-20 08:37 出处:网络
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 par

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 regene开发者_如何转开发rate 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号