我在将带有模型列表的ViewBag返回到PartialView时遇到了一些麻烦,PartialView是与ParentView不同的模型类型,但我希望仅在渲染的局部视图内呈现结果父视图。也许看着我的代码,会更好地理解。
这是我的控制器:
public ActionResult Search()
{
//ViewBag.Usuarios = db.User.ToList();
return View();
}
[HttpGet]
public ActionResult Pesquisar(UserFilter userFilter)
{
List<UserModel> retorno = new List<UserModel>();
ViewBag.Mensagem = "Não foi encontrado registro com os filtros informados";
if (userFilter.Name != null)
{
retorno = db.User.Where(x => x.Name.Contains(userFilter.Name)).ToList();
if (retorno != null)
{
ViewBag.Usuarios = retorno;
return PartialView("Search", ViewBag.Usuarios);
}
return View("Search", ViewBag.Mensagem);
}
if (userFilter.UserID != 0)
{
UserModel retorn = new UserModel();
var id = Convert.ToInt16(userFilter.UserID);
retorn = db.User.FirstOrDefault(x => x.UserID == id);
return View("Details", retorn);
}
return View("Search", ViewBag.Mensagem);
}
这是我的父视图
@model Sistema_ADFP.Filters.UserFilter
<body>
<div class="container">
<h2>Buscar Usuário</h2>
<form role="form" method="GET" action="/User/Pesquisar">
<div class="col-lg-12">
<div class="col-lg-3">
<div class="form-group">
<label>Nome</label>
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
<label>ID</label>
@Html.EditorFor(model => model.UserID, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
<label>CPF</label>
@Html.EditorFor(model => model.CPF, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
</div>
<div class="clearfix"></div>
<div class="form-group pull-right">
<label> </label>
<input type="submit" class="btn btn-primary"/>
</div>
</form>
</div>
<div class="clearfix"></div>
@{
if (ViewBag.Mensagem == null)
{
<div id="resultadoLista">
@{
Html.RenderPartial("_List");
}
</div>
}
else
{
<label>Nenhum registro encontrado</label>
}
}
</body>
这是我的部分视图
<h4>Usuários</h4>
<div class="table-responsive">
<table class="table">
<tr class="inverse" align="center">
<th>Nome</th>
<th>Sexo</th>
<th>Estado Civil</th>
<th>Educação</th>
<th>Profissão</th>
<th>Voluntário</th>
<th>Data Nascimento</th>
<th>Ações</th>
</tr>
@if (ViewBag.Usuarios != null)
{
foreach (var item in ViewBag.Usuarios)
{
<tr class="active">
<td data-th="Nome"><a class="modal-ajax-link" href="#test-popup">@item.Name</a></td>
<td data-th="Sexo">@item.Sex</td>
<td data-th="EstadoCivil">@item.MaritalStatus</td>
<td data-th="Education" align="center">@item.Education.Description</td>
<td data-th="Education" align="center">@item.Profession.Name</td>
@if (item.Voluntary)
{
<td data-th="Voluntario" align="center">Ativo</td>
}
else
{
<td data-th="Voluntario" align="center">Inativo</td>
}
<td data-th="DataNasc" align="center">@item.BirthDate</td>
@* data-mfp-src="@HttpContext.Current.Request.Url.Host:@HttpContext.Current.Request.Url.Port/User/Details/2" *@
<td data-th="Ações" align="center">
<a class="btn btn-info modal-ajax-link" href='@Html.ActionLink("Editar", "Edit",
new { id = @item.UserID })'><i class="icon_pencil"></i></a>
<a class="btn btn-danger modal-ajax-link" href="#delete-modal"><i class="icon_trash_alt"></i></a>
</td>
</tr>
}
}
</table>
</div>
我得到的错误是:
传递到字典中的模型项是类型的 'System.Collections.Generic.List`1 [Sistema_ADFP.Models.UserModel]', 但是这个字典需要一个类型的模型项 'Sistema_ADFP.Filters.UserFilter'。
我理解错误的原因,但我无法找到重置流程并使其工作的方法。有没有人知道或得到我能做什么的提示?
答案 0 :(得分:3)
解决此问题的方法是不使用ViewBag并为您的视图创建ViewModel。 ViewModel是一个视图类,它将为您的视图提供所有必需的属性。因此,为了示例,您可以使用名为:
的视图模型public class WrapperVM
{
public UserFilter Filter {get; set;}
public UserModel Model {get; set;}
}
因此,填充这些属性并将WrapperVM传递给父视图(更改为@model WrapperVM),然后您可以将usermodel传递给局部视图。我希望这会有所帮助。