[英]loading and updating a view with two partialview
使用C#,MVC,Razor
我很難掌握如何在兩個局部視圖上執行模型加載,以及在用戶輸入時使用新信息更新這些視圖。
我有一個包含以下內容的常規視圖:
@model Myapp.ViewModels.SearchedUser
<body>
<div id="tabs">
<ul>
<li><a href="#tabs-1">User</a></li>
<li><a href="#tabs-2">Groups</a></li>
</ul>
<div id="tabs-1">
@Html.Partial("_Edit");
</div>
<div id="tabs-2">
@{Html.Partial("_Groups");}
</div>
</div>
最初,用戶顯示在主視圖上。 但是,我需要從用戶更新/添加/刪除組,並在添加該代碼時開始將其移向選項卡式頁面。
因此,在要編輯的局部視圖上,我有:
@model Myapp.ViewModels.SearchedUser
...display code for user
我覺得這里的@model從主視圖看可能是多余的。
我的小組局部視圖如下所示:
@model Myapp.ViewModels.AllGroups
<!--at if (Model.AddGroups != null)
{ -->
<h2>Groups to be Added</h2>
<table id="AddGroupList">
<thead>
<tr>
<th>
Name
</th>
<th>
Until
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
@foreach (var Group in Model.CurrentGroups)
{
<tr>
<td>
@Html.DisplayFor(model=> Group.GroupName)
</td>
<td>
@Html.DisplayFor(model=> Group.EndDate)
</td>
<td>
@Html.DisplayFor(model=> Group.bRemove)
</td>
<td>
@if (AuthCheck.CheckUser(new HttpContextWrapper(HttpContext.Current), oRoles.StaffChangeRequest) ||
AuthCheck.CheckUser(new HttpContextWrapper(HttpContext.Current), oRoles.StudentChangeRequest))
{
<span>@Html.ActionLink("Remove", "RemoveGroup", new { id = Group.ID })</span>
}
</td>
</tr>
}
</tbody>
</table>
我確實需要將IDnum傳遞給每個局部視圖,以為該用戶加載當前模型。
現在的問題是...
我最好將組添加到SearchedUser模型中,並在每次向用戶添加組時重新加載整個模型嗎?
我不確定我是否理解您的問題。 如果您詢問如何渲染包含兩個具有不同模型的部分視圖調用的視圖,那么您應該擁有一個主視圖,該主視圖的視圖模型包含兩個其他視圖模型。
class MainViewModel
{
PartialOneViewModel {get;set;}
PartialTwoViewModel {get;set;}
}
class PartialOneViewModel
{
// props
}
class PartialTwoViewModel
{
// props
}
主視圖模型應具有:@model MainViewModel
零件應該具有自己的視圖模型。 您的帖子應該都收到帶有兩個部分的“ MainViewModel”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.