Open partial view by passing parameter

Asked

Viewed 785 times

1

I am doing some tests on ASP . NET CORE 2.0 and I have the following question. I have an Index (View) that is displaying a list of `Users`.

And from the click of a button `Edit`, I rewrite my Partial View inside a modal, where this Partial is strongly typed. Problem is when I open my modal and ends up displaying error that the types are different, IE, the partial view receives Usuariomodel but my index is typed with Ienumerable.

From this I do not know how to pass the parameter to my partial. Follow the code of the two items:

Index

@model IEnumerable<MyProjecy.UI.Models.UsuarioModel>

<div class="panel panel-default">
    <div class="panel-heading text-center"><h3>Lista de Usuários</h3></div>
    <table class="table">
        <thead>
            <tr>
                <th style="width: 100px;"></th>
                <th style="width: 80px;">Id</th>
                <th>@Html.DisplayNameFor(model => model.Usuario_Login)</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        <a class="btn btn-default btn-sm" data-toggle="modal" data-target="#exampleModal" data-usuarioId="@item.Usuario_Id" data-usuarioLogin="@item.Usuario_Login">
                            <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
                        </a>
                        <button type="button" class="btn btn-danger btn-sm">
                            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                        </button>
                    </td>
                    <th>@item.Usuario_Id</th>
                    <td>@item.Usuario_Login</td>
                </tr>
            }
        </tbody>
    </table>
</div>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  @await Html.PartialAsync("_EditUserPartial")
</div><!-- /.modal -->


@section scripts {
@* NADA AQUI *@
}

Partial

@model MyProject.Models.UsuarioModel

<h4>UsuarioModel</h4>
<hr />

<div class="modal-dialog" role="document">
    <div class="modal-content">
        <form asp-action="UsuarioUpdate" method="post">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="gridSystemModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">

                <div class="row">
                    <div class="col-md-4">
                        @Html.HiddenFor(m => m.Usuario_Id)
                        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                        <div class="form-group">
                            <label asp-for="Usuario_Login" class="control-label"></label>
                            <input asp-for="Usuario_Login" class="form-control" />
                            <span asp-validation-for="Usuario_Login" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label asp-for="Usuario_Senha" class="control-label"></label>
                            <input type="password" asp-for="Usuario_Senha" class="form-control" />
                            <span asp-validation-for="Usuario_Senha" class="text-danger"></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <a asp-controller="Usuario" asp-action="Index" id="btnCancelar" class="btn btn-danger">Cancelar</a>
                <input type="submit" value="Salvar" class="btn btn-success" />
            </div>
        </form>
    </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

1 answer

0


in that part

 @await Html.PartialAsync("_EditUserPartial")

Change to

  @await Html.PartialAsync("_EditUserPartial", new UsuarioModel())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.