Using two partial views in one view

Update 2: partialview

@model ASD.Models.StatisticsModel
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
{
    <table id="statisticstable">
        <thead>
            <tr>
                <th>Hour</th>
                <th>User</th>
                <th>Customer</th>
                <th>Order</th>
                <th>Rows</th>
                <th>Quantity</th>
            </tr>
        </thead>
        <tbody>
    </table>
}

Update 1

There was a typo in the name of partialView. A bug was fixed, and this error appeared:

Additional Information: The model element passed to the dictionary is of type "ASDWebPortalMVC.Models.LogModelVM", but this dictionary requires a model element of type "ASD.Models.StatisticsModel".


I use @ html.RenderPartial for a partial view.

LogModelsController.cs - (connected to LogModel)

[HttpPost]
public PartialViewResult LogPartialView()
{
    // Some other stuff 
    LogModelVM LMVM = new LogModelVM();
    return PartialView("_LogPartialLayout", LMVM);
}

Now I want to add another partial view using another model (StatisticsModel)


LogLayout.cshtml

@model ASDMVC.Models.LogModelVM

@* This is the working PartialView *@
<div id="log" class="tab">
    <h1>Log</h1>
    @using (Ajax.BeginForm("LogPartialView", "LogModelsController",
            new AjaxOptions
   {
       HttpMethod = "POST",
       InsertionMode = InsertionMode.Replace,
       UpdateTargetId = "divLogs"
   }, new
       {
           id = "NewTableId"
       }))
       {
           <p>@Html.TextBox("SearchString",null, new { @placeholder = "Message" })</p>
           if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
           {
               <p>
               @Html.DropDownListFor(m => m.SelectedCustomer, Model.CustomerList, new { @id = "logdropdownlabel" })
               </p>
           }
               <p><input type="submit" class="standardbutton logsearch" name="submit" value="Search" /></p> 
           }
           <div id="divLogs">
               @RenderBody()
               @Html.Raw(ViewBag.Data)
               @{Html.RenderPartial("_LogPartialLayout");}
           </div>
       </div>

@* This is the non-working PartialView. *@
<div id="statistics" class="tab">
    <h1>Statistics</h1>
    <div id="statistics">
        @{Html.RenderPartial("_StatisticsPartialView");}
    </div>
</div>

StatisticsController.cs (connected to the statistical module)

 [HttpPost]
 public PartialViewResult Statistics(string conn)
 {
     StatisticsModel STM = new StatisticsModel();
     StatisticsDbContext DbContext = new StatisticsDbContext(conn);
     return PartialView("_StatisticsPartialView", STM);
 }

I am new to this, so any help would be appreciated. :)

+4
1

, , :

@Html.Partial("~/Views/Shared/_StatisticsPartialView.cshtml")

@{ Html.RenderPartial("~/Views/Shared/_StatisticsPartialView.cshtml"); }

, !

: LogModelVM, :

@{Html.RenderPartial("_StatisticsPartialView", new ASD.Models.StatisticsModel());}
+3

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


All Articles