简体   繁体   中英

Partial View cannot access Model

I am new to MVC and trying a few things out, so I'd appreciate if someone can tell me where I am going wrong. I converted this from VB where it worked, but now, in C# it doesn't. Maybe my conversion is bad, but similar code is working in VB.

I pass the Model to the Index (which will later render several partials and I'll build a ViewModel to hold each different type of model). Then, I try to pass the Model to the Partial, but the Partial cannot access the Model in the foreach loop.

Below if the code. Any help would be appreciated.

Controller:

public ActionResult HospiceSummary(int? id)
    {
        IEnumerable<PatAdm> patadms = (from pa in db.PatAdms where pa.AdmNum == id select pa).ToList();
        return View(patadms);
    }

View:

@model IEnumerable<PatAdm>
@{
ViewBag.Title = "View";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>.... Some other code up here</h2>

<h2>Hospice</h2>
<div class="container-fluid">
    @Html.Partial("~/Views/Shared/_PatientAdmissionSearch.cshtml", Model)
</div>

Partial:

    @model IEnumerable<PatAdm>

<div class="row">

    <div class="col-xs-12">
        <div class="table-header">
            Find a Patient
        </div>

    </div> @*col-xs-12*@
</div> @*row1*@

<div class="row">


    @using (Html.BeginForm("Index", "Admission", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        <div class="col-md-4">
            <div class="input-append">
                <input type="text" class="span2 search-query" id="query" name="query">
                <button type="submit" class="btn"><i class="icon-search"></i></button>
            </div>
        </div>

        <table class="table">
            <tr>
                <th></th>
                <th>Patient Name</th>
                <th>Acct #</th>
                <th>Admit Date</th>
                <th>Discharge Date</th>
                <th>DOB</th>

            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.ActionLink("Select", "HospiceSummary", "Hospice", new {id = item.AdmNum}, new { @class = "label label-info"}) <br />
                    </td>
                    <td>@Html.DisplayFor(modelItem => item.Patient.LName), @Html.DisplayFor(modelItem => item.Patient.FName)</td>
                    <td>@Html.DisplayFor(modelItem => item.AdmNum)</td>
                    <td>@Html.DisplayFor(modelItem => item.AdmDate)</td>
                    <td>@Html.DisplayFor(modelItem => item.DischDate)</td>
                    <td>@Html.DisplayFor(modelItem => item.Patient.DOB)</td>

                </tr>
            }

        </table>
    }


</div> @*row2*@

The model of Parent view is by default passed to the partial view, So you don't need to pass explicitly. Also make sure that the model on parent view has value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM