c# - Return different model on partial view -


i'm having trouble returning viewbag list of model partialview, different type of model parentview, wish render result, inside partial view rendered on parent view. perhaps looking @ code, give better understanding.

here controller:

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); } 

here parent view

@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>&ensp;</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> 

and here partial view

<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> 

the error i'm getting is:

the model item passed dictionary of type 'system.collections.generic.list`1[sistema_adfp.models.usermodel]', dictionary requires model item of type 'sistema_adfp.filters.userfilter'.

i understand error reason , can not means of resetting flow , make work. knows or have tip of can ?

the solution fix not using viewbag , create viewmodel view. viewmodel class view , have necessary properties view. so, sake of example, have view model called:

public class wrappervm {  public userfilter filter {get; set;}  public usermodel model {get; set;} } 

thus, fill these properties , pass wrappervm parent view (change @model wrappervm) , can pass usermodel partial view. hope helps.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -