Zero Display Model

I am trying to add a partial view inside a layout page.

Model

public class SummaryPanelModel
    {
        public int TotalDesignDocs { get; set; }
        public int TotalVendorDocs { get; set; }
        public int TotalBusinessDocs { get; set; }
        public int TotalManagementDocs { get; set; }
    }

SummaryPanel_Partial Partial View Controller:

 public ActionResult SummaryPanel_Partial()
        {
            rep = new SummaryRepository();
            SummaryPanelModel model = new SummaryPanelModel();
            model = rep.ReadsummaryPanel();//read from database
            return View(model);
        }

Layout Page

<!DOCTYPE html>
<html lang="en">
@{
    Layout = null;
}

 @Html.Partial("SummaryPanel_Partial")

SummaryPanel_Partial Partial View:

@model Doc.Web.Models.SummaryPanel.SummaryPanelModel

<div id="pnlBar">
    @Html.Label(Model.TotalDesignDocs.ToString())
<div/>

even though I passed the model in the controller action, the model always has a zero value in a partial view.

+4
source share
2 answers
@Html.Partial("SummaryPanel_Partial")

Calling partial in this way will not call the controller + action . Instead, it simply finds the view SummaryPanel_Partialand displays it. Since you are not currently creating a model, the model is null.

Instead, call Html.Actionwhich will trigger controller + action.

@Html.Action("SummaryPanel_Partial", "Controller")

:

public ActionResult SummaryPanel_Partial()
{
    // ...
    return PartialView(model);
}
+6

PartialViewResult

   public PartialViewResult SummaryPanel_Partial()
    {
       rep = new SummaryRepository();
        SummaryPanelModel model = new SummaryPanelModel();
        model = rep.ReadsummaryPanel();//read from database
        return PartialView(model);
    }
+1

Source: https://habr.com/ru/post/1532596/


All Articles