加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在两个不同的强类型视图中渲染一个局部视图

发布时间:2020-12-16 06:57:48 所属栏目:asp.Net 来源:网络整理
导读:我有一个强类型的Person视图,我想渲染一个部分: 人物视图(强类型为人物) label for="name"Name/label % Html.RenderPartial("AddressForm"); %/label AddressForm View(无类型,因为我也想在分发器强类型视图中使用它) 当我尝试从Person视图中调用此partial
我有一个强类型的Person视图,我想渲染一个部分:

人物视图(强类型为人物)

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

AddressForm View(无类型,因为我也想在分发器强类型视图中使用它)

当我尝试从Person视图中调用此partial时,我收到此错误:

编译器错误消息:CS1963:表达式树可能不包含动态操作

来源错误:

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>

如何让这部分渲染,以便我可以在多个其他类型中使用我的部分addressView?

编辑:

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

错误在上面.

解决方法

你不能在动态viewmodel中使用强类型助手:

您可以使用非强类型助手,如下所示:

<%@ 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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读