如何在 Html.RenderAction (MVC3) 中发送模型对象
我正在使用 MVC3 razor,并且尝试将对象传递给部分视图,但它不起作用。
这可以正常工作,无需将对象模型发送到部分视图:
Html.RenderAction("Index", "ViewName");
尝试这样做不会发送模型对象,而是得到空值(对象有数据,视图需要它):'
Html.RenderAction("Index", "ViewName", objectModel);
这是否可以使用 RenderAction?
谢谢!
编辑:我发现了错误,控制器的操作出现错误,没有拾取发送的对象。感谢您的帮助!
I'm using MVC3 razor, and I'm trying to pass an object to a partial view, and it's not working.
This works fine without sending the object model to the partial view:
Html.RenderAction("Index", "ViewName");
Trying this doesn't sent the model object, i'm getting nulls instead (the object has data, and the view expects it):'
Html.RenderAction("Index", "ViewName", objectModel);
Is this even possible using RenderAction?
Thanks!
Edit: I found the error, there was an error with the controller's action that didn't pick up the sent object. Thanks for all your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您可以使用 Action 将对象传递给控制器方法。这可以在任何可用视图上完成,例如,我在共享库中有一个视图,该库构建到引用我的共享项目的项目 bin 文件夹(属性 - 在 Visual Studio 中,如果视图文件上较新,则复制)。它的完成方式如下:
Controller:
MVC 页面(使用 Razor 语法):
或使用
RenderAction
方法:注意:在
@ 中Html.Action()
,Model
对象必须是MyModel
类型,并且第三个参数必须设置为控制器变量名称,其中我的是 <代码>MyModel m。m
是您必须分配的内容,因此我执行m = Model
。You can actually pass an object to a controller method using Action. This can be done on any avaialble view, for instance I have one in a shared library that gets built to project bin folders that reference my shared project (properties - Copy if newer on the view file, in Visual Studio). It is done like so:
Controller:
MVC page (using Razor syntax):
or using
RenderAction
method:Note: in the
@Html.Action()
, theModel
object must be of typeMyModel
and that 3rd parameter must be set to the controller variable name, of which mine isMyModel m
. Them
is what you must assign to, so I dom = Model
.假设您想将 foo 作为模型传递,
现在首先创建一个 ActionResult
调用它
say you want to pass
foo
as model, make it firstnow make an ActionResult
call it
通常,如果我已经有一个可用的模型,那么使用
Html.Partial
比尝试呈现操作更有意义。其中
Foo.cshtml
是一个视图文件(可能在您的共享文件夹中),使用@model FooProject.Models.FooModel
或任何您的模型的名称进行强类型化。这可以是您需要的复杂模型。 Model 是页面的主模型,您必须在其中设置FooModel
- 或者如果Foo
视图使用与父页面相同的模型,则忽略此参数。当您只有简单的参数时,RenderAction 通常会更好,因为您只是模拟对具有路由/查询字符串参数的常规操作的请求 - 然后将该响应转储到您的页面中。如果您需要在页面模型中不可用的布局中放置某些内容(例如侧栏中的元素),它会非常有效。
Usually if I have a model already available it makes more sense to use
Html.Partial
than trying to render an action.Where
Foo.cshtml
is a view file (perhaps in your Shared folder) strongly typed with with@model FooProject.Models.FooModel
or whatever your model is called. This can be as complex a model as you need it to be. Model is your page's main model into which you must setFooModel
- or just omit this parameter if theFoo
view uses the same model as the parent page.RenderAction
is generally better when you have just simple parameters, because you're just simulating a request to a regular action which has routing/query string parameters - and then dumping that response into your page. It works well if you need to put something in a Layout that isn't available in your page's model such as an element in a side bar.