views:

37

answers:

1

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.

A: 

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.

jfar
Sorry for the confusion. Everything works as far as the user makes a selection from the drop down and the correct partial class gets displayed. The problem is that within the partial views it doesn't appear that I have access to the parent views Model. My question is, if this is the best approach how can the parent views Model be passed to the partial views or is there a another approach? I hope this clears things up.
fynnbob