我使用mvc 4并尝试让我的Web应用程序更具动态性。目前我尝试在部分视图中分割一些视图,以便代码更易读,并且我可以更好地重新使用应用程序的某些部分。 所以,现在这导致我的问题。我有类似这样的一个观点:用局部视图中的按钮重新加载局部视图
<h1>Manage department</h1>
<div id="EmployeesManagement">@Html.Action("OpenEmployeesManagement")</div>
<div id="DepartmentManagement">@Html.Action("OpenDepartmentManagement")</div>
<div id="DepartmentTumorModels">@Html.Action("OpenDepartmentModels")</div>
每个那些都是其获得从控制器称为像部分观点:
public PartialViewResult OpenDepartmentModels()
{
ViewBag.ChangeVisibility = -1;
HoDManagementModel hoDManagementModel = new HoDManagementModel { UserWithRoleModelList = azaraUserManagement.GetAllEmployesOfHoD(user.getUserId()), OrganisationUnits = azaraUserManagement.GetAllOrganisationUnitsFromHoD(user.getUserId()) };
List<ModelWithOrganisationUnit> Models = ModelManagement.SearchModelsOfDepartment(hoDManagementModel.OrganisationUnits);
return PartialView("DepartmentModels", Models);
}
现在我的问题。我有这样一个局部视图:
@model List<Modelle.Models.BusinessTierObjects.Models.ModelWithOrganisationUnit>
<fieldset>
<legend>Manage the models of your department</legend>
<table class="tablesorter">
<thead>
<tr>
<th>ID</th>
<th>Name </th>
<th>Department </th>
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in @Model)
{
<tr>
<td>@item.ModelId</td>
<td>@Html.ActionLink((String)item.ModelName, "Details", "Details", new { id = item.ModelId }, null)</td>
<td>@item.OrganisationUnitName</td>
@if (ViewBag.ChangeVisibility == item.ModelId)
{
<td><select name="ChangeVisibility" id="ChangeVisibility">
<option value="Department" onclick="location.href='@Url.Action("ChangeVisibility", "ManageDepartment", new {tumorModelId = item.ModelId, Visibility = 0})'">Department</option>
option value="Coop" onclick="location.href='@Url.Action("ChangeVisibility", "ManageDepartment", new { ModelId = item.ModelId, Visibility = 2 })'">Coop</option>
<option value="WWW" onclick="location.href='@Url.Action("ChangeVisibility", "ManageDepartment", new { ModelId = item.ModelId, Visibility = 3 })'">WWW</option>
</select></td>
}
else{
switch ((byte)item.Visibility)
{
case 0: <td>Department</td>; break;
case 2: <td>Coop</td>; break;
case 3: <td>WWW</td>; break;
}
}
<td><button name="button" class="button" onclick="location.href='@Url.Action("RequestChangeVisibility", "ManageDepartment", new { change = @item.ModelId })'">Change Visibility</button>
</td>
</tr>}
</fieldset>
,如果我在最后一个按钮单击只是局部视图应该重新加载。但是,相反,应用程序仅向我显示浏览器中没有任何布局的局部视图。我做错了什么或不能解决我的问题? 从按钮控制器动作是:
public PartialViewResult RequestChangeVisibility(int change)
{
ViewBag.ChangeVisibility = change;
HoDManagementModel hoDManagementModel = new HoDManagementModel { UserWithRoleModelList = azaraUserManagement.GetAllEmployesOfHoD(user.getUserId()), OrganisationUnits = azaraUserManagement.GetAllOrganisationUnitsFromHoD(user.getUserId()) };
List<ModelWithOrganisationUnit> Models = ModelManagement.SearchModelsOfDepartment(hoDManagementModel.OrganisationUnits);
return PartialView("DepartmentModels", Models);
}
非常感谢你,你安全我的一天:)。现在一切都运行良好,所以我可以尝试以这种方式修改我的部分视图。也感谢您的教程。我搜索部分视图,但我几乎只有mvc 2教程。 –
您忘记了控制器功能。发现返回PartialViewResult并返回PartialView(“Blah”,blah);',其中“Blah”是视图名称,“blah”是模型的IEnumerable,在我的情况中,是要走的路,与替换.html()给出的答案组合在一起。基本上与OP中给出的RequestChangeVisibility函数非常相似,但不完全一样,因为我在局部视图中有一堆对象。完整的答案是赞赏的,所以确认部分是对的会有帮助。这个在线提供的指导太少。 –
vapcguy