Question: how to pass model from @html.partial

Question

how to pass model from @html.partial

Answers 1
Added at 2016-12-26 14:12
Tags
Question

below is my cshtml code

ive used IEnumerable in the main view and and single model object in partial view

im doing CRUD operation ...each row has edit/details/delete buttons

@model IEnumerable<jQuery_CRUD.DAL.User>
@foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContactNo)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.UserId }, new { @class = "modal-with-form btn btn-default" ,href = "#modalEdit" }) |
                @Html.ActionLink("Details", "Details", new { id = item.UserId }, new { @class = "modal-with-form btn btn-default", href = "#modalDetails" }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.UserId }, new { @class = "modal-with-form btn btn-default", href = "#modalDelete" })             



                <div id="modalEdit" class="modal-block modal-block-primary mfp-hide">
                    @Html.Partial("Edit", item)
                </div>

                <div id="modalDetails" class="modal-block modal-block-primary mfp-hide">
                    @Html.Partial("Details", item)
                </div>


                <div id="modalDelete" class="modal-block modal-block-primary mfp-hide">
                    @Html.Partial("Delete", item)
                </div>




            </td>
        </tr>

clicking on edit/details shows information only for the first row in the table

the model in the partial view i used is

@model jQuery_CRUD.DAL.User

HOw do i overcome this

Answers to

how to pass model from @html.partial

nr: #1 dodano: 2016-12-26 14:12

That's because you're re-using the same id attribute for each item in the table while element id should be unique.

Use the item.UserId to assign a unique id for each:

@Html.ActionLink("Edit", "Edit", new { id = item.UserId }, new { href = "#modalEdit" + item.UserId }) |
@Html.ActionLink("Details", "Details", new { id = item.UserId }, new { href = "#modalDetails" + item.UserId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.UserId }, new { href = "#modalDelete" + item.UserId })

<div id="modalEdit@(item.UserId)" class="modal-block modal-block-primary mfp-hide">
  @Html.Partial("Edit", item)
</div>

<div id="modalDetails@(item.UserId)" class="modal-block modal-block-primary mfp-hide">
  @Html.Partial("Details", item)
</div>

<div id="modalDelete@(item.UserId)" class="modal-block modal-block-primary mfp-hide">
  @Html.Partial("Delete", item)
</div>
Source Show
◀ Wstecz