0

i just encounter a code where a person showing how to keep webgrid in partial view and populate. see the link for complete code Bind WebGrid form AJAX

see their partial view code

<!-- Current Buildings -->
@{ 
    if (Model != null && Model.Count() > 0)
    {                            
        var grid = new WebGrid(source: Model, rowsPerPage: ThisController.PageSize, ajaxUpdateContainerId: "tabs-2", defaultSort: "BuildingNumber");
        grid.Bind(Model, rowCount: Model.Count(), autoSortAndPage: false);
        grid.Pager(WebGridPagerModes.All);

        grid.GetHtml(
            tableStyle: "display",
            alternatingRowStyle: "alt",
            columns: grid.Columns(
                grid.Column("BuildingNumber", header: "Building Number"),
                grid.Column("ConstructionDate", header: "Construction Date"),
                grid.Column("ExtSquareFeet", header: "Exterior Sq. Ft."),
                grid.Column("IntSquareFeet", header: "Interior Sq. Ft."),
                grid.Column("IU_Avail", header: "IU Available"),
                grid.Column("SpaceAvail", header: "Space Available"),
                grid.Column("FixedAssetValue", header: "Fixed Asset Value"),
                grid.Column("FixedEquipValue", header: "Fixed Equipment Value")
            )
        );   
    }
    else
    {
        @:There are no buildings at this facility.
    }
}   

but there is no reference for model at top like

@model IEnumerable<MyProject.Models.BuildingModel>

see how partial view understand which model they need to use to populate the webgrid.

see this code to bind webgrid

grid.Bind(Model, rowCount: Model.Count(), autoSortAndPage: false);

here the word model has been used so how web grid know about model because no reference has been added at top of partial view.

in main view they use this code to render partial view

@Html.Partial("_Buildings")

i do not know that model data no need to pass with

@Html.Partial("_Buildings")

or model data automatically pass to partial view.

please help me to understand this. thanks

Community
  • 1
  • 1
Thomas
  • 33,544
  • 126
  • 357
  • 626
  • You can think of it as an include. It means that there is a file named _Buildings.cshtml (if its Razor and C#) in the same folder with view HTML in it that will get included into the main view. See http://stackoverflow.com/questions/13934671/using-partial-views-in-asp-net-mvc-4 – SeanN Dec 24 '15 at 09:00
  • Sorry not very clear what u try to say. normally partial view stay in shared folder. are you trying to say in this case main view and partial view stay in same folder? – Thomas Dec 24 '15 at 10:07
  • You can put it wherever you like, just put the path in the code, see http://stackoverflow.com/questions/6060397/calling-html-partial-to-display-a-partial-view-belonging-to-a-different-control – SeanN Dec 24 '15 at 16:27

0 Answers0