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

发布于 2024-12-28 05:22:01 字数 356 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难理解 2025-01-04 05:22:01

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

Controller:

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

MVC 页面(使用 Razor 语法):

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

或使用 RenderAction 方法:

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

注意:在 @ 中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:

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

MVC page (using Razor syntax):

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

or using RenderAction method:

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

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

夢归不見 2025-01-04 05:22:01

假设您想将 foo 作为模型传递,

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

现在首先创建一个 ActionResult

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

调用它

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

say you want to pass foo as model, make it first

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

now make an ActionResult

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

call it

@Html.RenderAction("FooBar", "Controller", new { Name = "John", Age=20 });
誰ツ都不明白 2025-01-04 05:22:01

通常,如果我已经有一个可用的模型,那么使用 Html.Partial 比尝试呈现操作更有意义。

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

其中 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.

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

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 set FooModel - or just omit this parameter if the Foo 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文