asp.net-mvc 如何在 Html.RenderAction (MVC3) 中发送模型对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8959425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:41:56  来源:igfitidea点击:

How to send model object in Html.RenderAction (MVC3)

asp.net-mvcasp.net-mvc-3razorasp.net-mvc-partialview

提问by Michael

I'm using MVC3 razor, and I'm trying to pass an object to a partial view, and it's not working.

我正在使用 MVC3 razor,我试图将一个对象传递给局部视图,但它不起作用。

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?

这甚至可以使用 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!

编辑:我发现了错误,控制器的动作有一个错误,没有接收到发送的对象。感谢你的帮助!

回答by theJerm

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:

您实际上可以使用 Action 将对象传递给控制器​​方法。这可以在任何可用的视图上完成,例如我在一个共享库中有一个,它被构建到引用我的共享项目的项目 bin 文件夹中(属性 - 如果在 Visual Studio 中的视图文件上较新,则复制)。它是这样完成的:

Controller:

控制器:

public class GroovyController : Controller
{
    public ActionResult MyTestView(MyModel m)
    {
        var viewPath = @"~\bin\CommonViews\MyTestView";
        return View(viewPath, m);
    }
}

MVC page (using Razor syntax):

MVC 页面(使用 Razor 语法):

@Html.Action("MyTestView", "Groovy", new { m = Model })

or using RenderActionmethod:

或使用RenderAction方法:

@{ Html.RenderAction("MyTestAction", "MyTestController", new { area = "area", m = Model }); }

Note: in the @Html.Action(), the Modelobject must be of type MyModeland that 3rd parameter must be set to the controller variable name, of which mine is MyModel m. The mis what you must assign to, so I do m = Model.

注意:在 中@Html.Action()Model对象必须是类型MyModel并且第三个参数必须设置为控制器变量名称,其中我的是MyModel m。这m是你必须分配给的,所以我做m = Model

回答by John x

say you want to pass fooas model, make it first

说你想foo作为模型通过,先做

public class Foo {
    public string Name { get; set; }
    public int Age { get; set; }
}

now make an ActionResult

现在制作一个 ActionResult

public ActionResult FooBar(Foo _foo){
    return PartialView(_foo);
}

call it

称它为

@Html.RenderAction("FooBar", "Controller", new { Name = "John", Age=20 });

回答by Simon_Weaver

Usually if I have a model already available it makes more sense to use Html.Partialthan trying to render an action.

通常,如果我有一个可用的模型,那么使用它Html.Partial比尝试渲染一个动作更有意义。

@Html.Partial("Foo", Model.FooModel)

Where Foo.cshtmlis a view file (perhaps in your Shared folder) strongly typed with with @model FooProject.Models.FooModelor 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 set FooModel- or just omit this parameter if the Fooview uses the same model as the parent page.

Foo.cshtml强类型的视图文件(可能在您的共享文件夹中)在哪里,@model FooProject.Models.FooModel或者您的模型被称为什么。这可以是您需要的复杂模型。模型是您的页面的主模型,您必须将其设置到其中FooModel- 如果Foo视图使用与父页面相同的模型,则只需省略此参数。

RenderActionis 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.

RenderAction当您只有简单的参数时,通常会更好,因为您只是模拟对具有路由/查询字符串参数的常规操作的请求 - 然后将该响应转储到您的页面中。如果您需要在页面模型中不可用的布局(例如侧栏中的元素)中放置某些内容,则它可以很好地工作。