tags:

views:

227

answers:

2

I tried the following but doesnt work:

<%Html.RenderPartial("userControl",new {personID=Model.ID, name="SomeName"});%>

In the usercontrol I have a hidden field in an ajax form to which I assign personID. It wont compile, the hidden id is not recognized.

+1  A: 

You'd either have to use reflection or a helper class like RouteValueDictionary if you wanted to get the correct property from the anonymous type.

RouteValueDictionary is probably the easiest. Create an instance of it, passing the Model, and then use its index operator to query the values.

Eg:

<%
    var modelDictionary = new RouteValueDictionary(Model);
%>
<input type="hidden" name="personID" value="<%= modelDictionary["personID"] %>" />
Jon Benedicto
+1  A: 

I am not sure why would you want to do that, but here is how (strongly type Model is much better):

<%
    ViewData["PersonID"] = Model.ID;
    ViewData["Name"] = "SomeName";
    Response.Write(
        Html.RenderPartial("userControl"));
%>

OR

If you just do this:

<%=Html.RenderPartial("userControl")%>

and if your "userControl" is also strongly typed, it should be able to read "Model.ID"

Johannes Setiabudi