asp.net-mvc - 将两个值传递给 View

标签 asp.net-mvc

好的,我很确定我知道这个问题的答案,但以防万一。

我知道我可以将复合类传递给 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/

相关文章:

asp.net-mvc - ASP.NET MVC - 状态和体系结构

c# - 读取 PDF 文档作为指定要呈现的 PDF 的一部分

asp.net - 哪里可以获得 MVC4 的源代码

asp.net-mvc - 正确处理 ASP.NET MVC 中的自定义错误?

javascript - 将 knockout.js observablearray 对象传递给 MVC Controller Action?

c# - 尝试添加一对多关系时出现 NullReferenceException

javascript - jQuery、Asp.net -jAutoCalc : Implement auto calculation on the form to be appended in the table using jQuery

c# - 为什么导航栏菜单项不显示在 twitter.bootstrap.mvc4 包示例应用程序中?

c# - 帮助开始使用 VS2010 构建 ASP.NET 网站

asp.net-mvc - ASP.NET MVC 一条路线,两种不同的观点