Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering one partial view in two different strongly typed views

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

Person View (strongly typed as person)

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

AddressForm View (untyped, because I also want to use this in the Distributor strongly typed view)

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

Compiler Error Message: CS1963: An expression tree may not contain a 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 to render so that I can use my partial addressView across multiple other types?

Edited:

// 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>

Error is above.

like image 316
Andy Poquette Avatar asked Oct 04 '10 17:10

Andy Poquette


People also ask

Which are the methods are used to render partial view?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is strongly typed partial view?

Strongly Typed Partial View - When creating partial view, if we select or enter a type for the Model Class option then it will create strongly typed partial view. Now partial view file is created and it only contains the @model tag to specify the view model type.

What is a view that renders a portion of view content called?

Partial view in ASP.NET MVC is special view which renders a portion of view content. It is just like a user control of a web form application. Partial can be reusable in multiple views. It helps us to reduce code duplication.

Can we use multiple partial view in MVC?

You can only return one value from a function so you can't return multiple partials from one action method. If you are trying to return two models to one view, create a view model that contains both of the models that you want to send, and make your view's model the new ViewModel. E.g.


2 Answers

you can't use strongly typed helpers with dynamic viewmodel:

you can use non strongly typed helpers instead, like this:

<%@ 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>
like image 104
Omu Avatar answered Oct 18 '22 01:10

Omu


Couldn't you simply keep all your views strongly typed by letting both the model types that you want to render in your AddressForm view implement an interface, and let the AddressForm partial use that interface as its model type?

like image 2
Jonas Høgh Avatar answered Oct 18 '22 00:10

Jonas Høgh