在ASP.NET MVC局部视图 [英] partial views in ASP.NET MVC

查看:107
本文介绍了在ASP.NET MVC局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个由三个部分视图页面查看页面?我使用ASP.NET MVC

How create View page which is composed of three partial view pages? I am using ASP.NET MVC

    //
    // GET: /Partial/

    public ActionResult View1()
    {
        var upit = from i in proba.name
                   select i;
        return PartialView("upit",proba.name);
    }

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>
<div><%Html.RenderPartial("View1",proba.name); %></div>
</asp:Content>

为什么这个code返回错误:对象引用不设置到对象的实例

Why this code return error: Object reference not set to an instance of an object.

推荐答案

我想你想使用的RenderAction,不是的RenderPartial。视图1是一个动作,而不是一个视图。它返回的局部视图, upit 。注意,这需要MVC2(或MVC1 +期货)。你也可能想,除非你打算以及来自AJAX调用它来装饰与ChildActionOnlyAttribute的动作。

I think you want to use RenderAction, not RenderPartial. View1 is an action, not a view. It returns the partial view, upit. Note that this requires MVC2 (or MVC1 + futures). You also probably want to decorate the action with the ChildActionOnlyAttribute unless you intend to call it from AJAX as well.

[ChildActionOnly]
public ActionResult View1()
{
    var upit = from i in proba.name
               select i;
    return PartialView("upit",proba.name);
}


asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>
<div><%= Html.Action("View1"); %></div>
</asp:Content>

你所得到的特定错误的原因是, PROBA 变量没有在视图中定义,也不需要如此。在你的情况下,动作决定了被传递到局部视图的模型,所以没有必要要传递的任何数据。呈现一个动作时,你可以仅通过查询参数传递也无妨。

The reason you are getting the specific error is that the proba variable isn't defined in the view, nor does it need to be. In your case, the action determines the model being passed to the partial view so there's no need for any data to be passed. You could only pass it via query parameters anyway when rendering an action.

这篇关于在ASP.NET MVC局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆