简体   繁体   English

如何在ASP.NET MVC中使用部分视图模型

[英]How to use Partial View Model in ASP.NET MVC

I have a controller named DaftarController that calls Index view and fill it with mode.l 我有一个名为DaftarController的控制器,该控制器调用Index视图并用mode.l填充它

DaftarController: DaftarController:

    public ActionResult Index()
    {
        List<EventRecord> li = ws.GetEvents().ToList();
        var ura = li;
        return View(ura);
    }

It shows perfectly, but I want partial view inside my Index view. 它显示得很完美,但是我想在Index视图中使用局部视图。

@Html.Partial("~/Views/Daftar/_Deleted.cshtml");

So I add this in my DaftarController: 所以我将其添加到我的DaftarController中:

    public ActionResult _Deleted()
    {
        List<DeletedRecord> li = ws.GetDeleteds().ToList();
        var ura = li;
        return View(ura);
    }

But it gives error. 但这会带来错误。 I'm still confuse how to show partial view with model in it? 我仍然困惑如何在其中显示带有模型的局部视图?

If you want to call an action even though the action will return a partial view, you should use. 如果即使该操作将返回部分视图,也要调用该操作,则应使用。

@Html.Action("_Deleted", "Daftar") // Assume _Deleted is inside DaftarController

This will call the action then returns the view, and in your _Deleted action, you need to return it with PartialView method otherwise the layout will be included as the result. 这将调用该操作,然后返回该视图,在_Deleted动作中,您需要使用PartialView方法返回该视图,否则将包括布局作为结果。

public ActionResult _Deleted()
{
    List<DeletedRecord> li = ws.GetDeleteds().ToList();
    var ura = li;
    return PartialView(ura); // Not View(ura)
}

If you directly call the @Html.PartialView , that means you directly render the view without going to the action. 如果直接调用@Html.PartialView ,则意味着直接渲染视图而无需执行操作。

When you are defining a partial view to use in a razor view, you do not define the path with the file extension. 定义要在剃刀视图中使用的局部视图时,请勿使用文件扩展名定义路径。

So for your partial, it would be: 所以对于您的部分来说,它将是:

@Html.Partial("~/Views/Daftar/_Deleted");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM