How to return a PartialView from Core 2 RazorPage ViewModel Handler

前端 未结 2 1701
北荒
北荒 2021-01-04 21:11

In Asp.Net MVC, you can easily return a partial view by doing the following:

return PartialView(\"ModelName\", Model);

How is this done on a Raz

2条回答
  •  旧时难觅i
    2021-01-04 21:51

    Thanks alot to TechFisher for figuring it out, here is a bit cleaner example.

    public IActionResult OnGetTestPartial()
    {
        return new PartialViewResult()
        {
            ViewName = "Test",
            ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
            {
                Model = new TestPartialData { Data = "inputhere" },
            }
        };
    }
    

    Partial view in a file name "Test.cshtml" in the same folder as the above class.

    @using YourNamespace
    @model TestPartialData
    
    
    Hello, model value: @Model.Data

    Load it async with jquery

    $("#someHtmlElementId").load("Your/Path/TestPartial");
    

提交回复
热议问题