简体   繁体   English

如何显示局部视图

[英]How to show partial view

I have a page with information, i want to add there a list (in a form of table) within a partial view. 我有一个包含信息的页面,我想在局部视图中添加一个列表(以表格的形式)。 User has to have an ability to sort it by switching radio box. 用户必须具有通过切换单选框对其进行排序的功能。

My problem: the code works fine (i have tried in a separate view), but when i try to switch radio button (they submit page on change and activate 2nd method, which create new model according to radio button) i get html code only from my partial view. 我的问题:代码工作正常(我已经在单独的视图中尝试过),但是当我尝试切换单选按钮(它们在更改时提交页面并激活第二种方法,即根据单选按钮创建新模型)时,我只得到html代码从我的偏见来看。

In other words: 换一种说法:
i want to : HTML from view1 + HTML from myPartial 我想: view1 HTML + myPartial HTML
i get: only HTML from myPartial 我得到:只有myPartial HTML
I suppose problem is here (calling my myPartial ): 我想问题出在这里(称为myPartial ):

@Html.Action("_ShowEmployeeProjects", "Employee")

But when i try to use this: 但是当我尝试使用此:

@Html.Partial("Partial/_ShowEmployeeProjects")

I get this: 我得到这个:

The model item passed into the dictionary is of type 传递到字典中的模型项的类型为
'BTGHRM.Models.EmployeeJobDataViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[BTGHRM.Models.EmployeeProjectHistoryModel]'. 'BTGHRM.Models.EmployeeJobDataViewModel',但此字典需要类型为'System.Collections.Generic.List`1 [BTGHRM.Models.EmployeeProjectHistoryModel]'的模型项。

My controllers code: 我的控制器代码:

    public PartialViewResult _ShowEmployeeProjects() 
    {
        int EmpId = HRMSession.SelectedEmployeeId;

        using (var db = new HRMEntities())
        {
            List<EmployeeProjectHistoryModel> list = (from t1 in db.ProjectWorkers
                                                      join t2 in db.Projects
                                                      on t1.ProjectId equals t2.ProjectId
                                                      where (t1.WorkerId == EmpId && t1.IsActive == true)
                                                      select new EmployeeProjectHistoryModel()
                                                      {
                                                          ProjectName = t2.ProjectName,
                                                          Activity = t1.Activity,
                                                          StartDate = t1.StartDate,
                                                          EndDate = t1.EndDate
                                                      }).ToList();
            return PartialView("Partial/_ShowEmployeeProjects",list);
        }
    }
    [HttpPost]
    public PartialViewResult _ShowEmployeeProjects(string ActiveOnlySelect)
    {
        int EmpId = HRMSession.SelectedEmployeeId;
        List<EmployeeProjectHistoryModel> list;
        using (var db = new HRMEntities())
        {
            if (ActiveOnlySelect.Equals("both"))
            {
                list = (from t1 in db.ProjectWorkers
                        join t2 in db.Projects
                        on t1.ProjectId equals t2.ProjectId
                        where (t1.WorkerId == EmpId)
                        select new EmployeeProjectHistoryModel()
                        {
                            ProjectName = t2.ProjectName,
                            Activity = t1.Activity,
                            StartDate = t1.StartDate,
                            EndDate = t1.EndDate
                        }).ToList();
                list.OrderBy(x => x.StartDate);
            }
            else
            {
                list = (from t1 in db.ProjectWorkers
                        join t2 in db.Projects
                        on t1.ProjectId equals t2.ProjectId
                        where (t1.WorkerId == EmpId && t1.IsActive == true)
                        select new EmployeeProjectHistoryModel()
                        {
                            ProjectName = t2.ProjectName,
                            Activity = t1.Activity,
                            StartDate = t1.StartDate,
                            EndDate = t1.EndDate
                        }).ToList();
                list.OrderBy(x => x.StartDate);
            }
        }
        return PartialView("Partial/_ShowEmployeeProjects", list);
    }

My partial: 我的部分:

@model    List<BTGHRM.Models.EmployeeProjectHistoryModel>
@using (Html.BeginForm("_ShowEmployeeProjects", "Employee", FormMethod.Post, new { type = "main" }))
{
<table>
    <tr>
        <td>
            @Html.RadioButton("ActiveOnlySelect", "activeonly", true, new { id = "ActiveOnlySelect0", onchange = "this.form.submit();" })
            <label for="ActiveOnlySelect0">@Resources.Localization.show_only_actual</label>
        </td>
    </tr>
    <tr>
        <td>
            @Html.RadioButton("ActiveOnlySelect", "both", new { id = "ActiveOnlySelect1", onchange = "this.form.submit();" })
            <label for="ActiveOnlySelect1">@Resources.Localization.show_all_data</label>
        </td>
    </tr>
</table>
}
@{
    WebGrid grid = new WebGrid(Model, canSort: false, rowsPerPage: 15);
if (Model.Any())
{

    @grid.GetHtml(
    tableStyle: "table",
    headerStyle: "table_HeaderStyle",
    footerStyle: "table_PagerStyle",
    rowStyle: "table_RowStyle",
    alternatingRowStyle: "table_AlternatingRowStyle",
    selectedRowStyle: "table_SelectedRowStyle",
    columns: grid.Columns(
        grid.Column("ProjectName", Resources.Localization.project, style: "p30"),
        grid.Column("Activity", Resources.Localization.activity, style: "p30"),
        grid.Column("StartDate", Resources.Localization.start_date, format: @<text>
            @if (item.StartDate != null)
            {
                <span class="display-mode"><label id="StartDateLabel">@item.StartDate.ToShortDateString()</label></span>
                @Html.Hidden("Model.StartDate", (object)item.StartDate.ToShortDateString())
            }
            else
            {
                <span> &nbsp; </span>
            }</text>, style: "p10"),
        grid.Column("EndDate", Resources.Localization.end_date, format: @<text>
            @if (item.EndDate != null)
            {
                <span class="display-mode"><label id="EndDateLabel">@item.EndDate.ToShortDateString()</label></span>
                @Html.Hidden("Model.EndDate", (object)item.EndDate.ToShortDateString())
            }
            else
            {
                <span> &nbsp; </span>
            }</text>, style: "p10")
    )
    )
}

} }

It looks like you are passing a wrong model to your partial. 看来您要将错误的模型传递给您的局部模型。 The structure will be: 结构将是:

In your principal Layout: 在您的主要布局中:

@model System.Collections.Generic.List[BTGHRM.Models.your_model]
<!-- DO YOUR HTML´S STUFF. You can access to your_model.employee´s list -->
@Html.Partial("Partial/_ShowEmployeeProjects", Model.projects)

In your Partial, remember to get the model which you are passing from your main layout: 在Partial中,请记住从主布局获取要传递的模型:

@model System.Collections.Generic.List[BTGHRM.Models.your_model.projects]
<!-- DO YOUR HTML´S STUFF -->

Then, in your controller, you must return return: 然后,在您的控制器中,您必须返回return:

[HttpPost]
    public PartialViewResult _ShowEmployeeProjects(string ActiveOnlySelect)
    {
        // DO YOUR MAGIC   
        // model should be a List[BTGHRM.Models.your_model]
        PartialView("Partial/_ShowEmployeeProjects", model);
    }

Model: 模型:

public class your_model
{
   List<Employee> employees;
   List<Project> projects;
.....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM