Providing one partial view in two different strongly typed views

I have a strongly typed Person view that I want to make partial in:

Type of person (strongly typed as a person)

<label for="name">Name</label> <% Html.RenderPartial("AddressForm"); %> </label> 

AddressForm View (untyped because I also want to use it in a strongly typed Distributor view)

When I try to call this partial from the Person view, I get this error:

Compiler error message: CS1963: expression tree cannot contain dynamic operation

Source Error:

 Line 8: </div> Line 9: <div class="editor-field"> Line 10: <%= Html.TextBoxFor(model => model.addressLine1) %> Line 11: <%: Html.ValidationMessageFor(model => model.addressLine1) %> Line 12: </div> 

How can I get this partial render so that I can use my partial addressView for several other types?

Edited by:

 // GET: /Person/Create public ActionResult Create() { Person person = new Person(); return View(person); } //Person create view <% Html.RenderPartial("AddressForm"); %> //AddressForm Partial <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %> <fieldset> <legend>Address</legend> <div class="editor-label"> <label for="addressLine1" class="addressLabel">Address Line 1</label> </div> <div class="editor-field"> <%= Html.TextBoxFor(model => model.addressLine1) %> <%: Html.ValidationMessageFor(model => model.addressLine1) %> </div> </fieldset> 

The error is above.

+4
source share
2 answers

you cannot use strongly typed helpers with a dynamic view model:

you can use non-strongly typed helpers, for example:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <fieldset> <legend>Address</legend> <div class="editor-label"> <label for="addressLine1" class="addressLabel"> Address Line 1</label> </div> <div class="editor-field"> <%= Html.TextBox("addressLine1") %> <%: Html.ValidationMessage("addressLine1") %> </div> </fieldset> 
+2
source

Could you just keep all your views strictly typed by allowing both types of models that you want to visualize in your AddressForm view to implement an interface, and let ParticForm partially use this interface as its model type?

+2
source

All Articles