Abraham Putra Prakasa
Abraham Putra Prakasa

Reputation: 608

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:

    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.

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

So I add this in my 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?

Upvotes: 1

Views: 1547

Answers (2)

Yuliam Chandra
Yuliam Chandra

Reputation: 14640

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.

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.

Upvotes: 3

Thewads
Thewads

Reputation: 5053

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");

Upvotes: 2

Related Questions