我有一个包含信息的页面,我想在局部视图中添加一个列表(以表格的形式)。用户必须能够通过切换无线电盒来对其进行排序。
我的问题:代码工作正常(我已尝试在单独的视图中),但当我尝试切换单选按钮(他们提交页面更改并激活第二种方法,根据单选按钮创建新模型)我得到html代码仅来自我的部分视图。
换句话说:
我想:来自view1
的HTML +来自myPartial
的HTML
我得到:只有来自myPartial
的HTML
我想问题就在这里(打电话给我myPartial
):
@Html.Action("_ShowEmployeeProjects", "Employee")
但是当我尝试使用它时:
@Html.Partial("Partial/_ShowEmployeeProjects")
我明白了:
传递到字典中的模型项的类型为
'BTGHRM.Models.EmployeeJobDataViewModel',但这本词典需要 类型的模型项 'System.Collections.Generic.List`1 [BTGHRM.Models.EmployeeProjectHistoryModel]'。
我的控制器代码:
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);
}
我的部分:
@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> </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> </span>
}</text>, style: "p10")
)
)
}
}
答案 0 :(得分:1)
看起来你正在向你的部分传递一个错误的模型。结构将是:
在您的主要布局中:
@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)
在Partial中,请记住从主布局中获取要传递的模型:
@model System.Collections.Generic.List[BTGHRM.Models.your_model.projects]
<!-- DO YOUR HTML´S STUFF -->
然后,在您的控制器中,您必须返回return:
[HttpPost]
public PartialViewResult _ShowEmployeeProjects(string ActiveOnlySelect)
{
// DO YOUR MAGIC
// model should be a List[BTGHRM.Models.your_model]
PartialView("Partial/_ShowEmployeeProjects", model);
}
型号:
public class your_model
{
List<Employee> employees;
List<Project> projects;
.....
}