好的,我很确定我知道这个问题的答案,但以防万一。
我知道我可以将复合类传递给 View /局部 View 。不过,我的问题是我是否可以在没有模型的情况下传入对象。
所以类似于 Html.RenderPartial("MyPartialView", new { id=10, name="slappy" });
如果可以的话,局部 View 会是什么样子?
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<?????>" %>
我将如何访问这些属性?
Model.id???
编辑
我认为这是基于@Graphain 的解决方案的跟进;
<% Html.RenderPartial("PagedList", new { id="10" } ); %>
在我的部分 View 中,我有这个;
[<%= ViewData["id"] %>]
呈现 [];
然而这;
<%= Html.TextBox("id") %>
给我一个包含数字 10 的文本框。
最佳答案
当然可以。
您的部分 View 声明应如下所示:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
您将像这样访问属性:
<%=ViewData.Eval("id")%>
您将使用 ViewUserControl而不是 ViewUserControl(Of TModel) .
这意味着您将访问 ViewDataDictionary而不是 ViewDataDictionary(Of TModel)并且将只处理常规对象数据而不是强类型数据(以及字符串键而不是强类型属性名称)。
This link是对差异的一个很好的介绍:
The first type of ViewDataDictionary is an untyped collection consisting of a string and an object. The string represents the key, and the object holds the actual data. Like any untyped collection, the object can be of any type, but must be cast to the correct type before it is used. Items are referenced via their key in the ViewData property of the ViewUserControl (in the case of a PartialView). The second is a strongly typed collection, where items are properties of the ViewUserControl.Model property.
关于asp.net-mvc - 将两个值传递给 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3082106/