gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:54:34 26 4
gpt4 key购买 nike

我有一个强类型的 Person View ,我想在其中呈现部分:

人物 View (强类型为人物)

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

AddressForm View (无类型,因为我也想在 Distributor 强类型 View 中使用它)

当我尝试从 Person View 调用此部分时,出现此错误:

编译器错误消息: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>

错误在上面。

最佳答案

您不能在动态 View 模型中使用强类型助手:

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

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

关于asp.net-mvc - 在两个不同的强类型 View 中渲染一个局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3857705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com