简体   繁体   中英

Partial View Not Rendering Inside View

I have a partial view inside another partial view which, when first running the application loads as expected, but when you click to reload the view to push a model into it, it then renders as it's own completely separate view as if it weren't a partial.

I'm calling it inside an Ajax Form like so (On the Action link click, the _GetSearchModal method):

<div id="modelSearch">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">
                <i class="fa fa-search"></i> Search by Model / Manufacturer
            </h3>
        </div>
        <div class="panel-body">
            @using (Ajax.BeginForm("_GetSearch", "Home", new AjaxOptions() {UpdateTargetId = "modelSearch"}))
            {
                @Html.AntiForgeryToken()
                <div class="input-group">
                    @Html.TextBox("search", null, new {id = "name", @class = "form-control", placeholder = "Please enter a manufacturer or model"})
                    <span class="input-group-btn">
                        <button id="search" class="btn btn-default" type="submit"><i class="fa fa-search"></i></button>
                    </span>
                </div>
                if (Model != null)
                {
                    <div id="searchResults" class="fade-in two">
                        @foreach (var s in Model)
                        {
                            <div class="result">
                                @switch (s.ResultType)
                                {
                                    case "Man":
                                        <a href="#">@s.Manufacturer</a>
                                        break;
                                    case "Mod":
                                        @Html.ActionLink(s.Manufacturer + Html.Raw(s.Model), "_GetSearchModal", "Home", new {id = s.MachineId}, new {toggle = "modal", data_target = "#MachineModal"})
                                        <img src="~/Images/General/Tier/@(s.TierId).png" alt="Tier @s.TierId"/>
                                        break;
                                }

                            </div>

                        }
                    </div>
                }
            }
        </div>
    </div>
</div>

<!-- Product Modal -->
<div class="modal fade" id="MachineModal" tabindex="-1" role="dialog" aria-labelledby="MachineModalLabel">
    @Html.Partial("_SearchModal", new MachineModal())
</div>

And the view itself should load a different view model (MachineModal):

@model SpecCheck.Portals.Web.UI.ViewModels.MachineModal

@if (Model != null)
{

        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <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="MachineModalLabel">@Model.Manufacturer @Model.Model</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <img src="~/Images/@Model.Manufacturer/logo.png" alt="@Model.Manufacturer" /><br />
                            <a href="#" type="button" class="btn button">Wiki</a>
                        </div>
                        <div class="col-md-6">
                            @Model.Catagory1 | @Model.Category2<br /><br />
                            <span class="modal-em">Region: </span> @Model.Region<br />
                            <span class="modal-em">Status: </span>@Model.Status<br />
                            <span class="modal-em">Spec Date: </span>@Model.SpecDate
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <a href="#" type="button" class="btn button">View</a>
                    <a href="#" type="button" class="btn button">Quick Compare</a>
                    <a href="#" type="button" class="btn button">Compare</a>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
}

And the action to do this in the "Home Controller" is:

public ActionResult _GetSearchModal(string machineId)
{
    using (var db = new SpecCheckDbContext())
    {
        MachineModal machine = new MachineModal();
        var searchHelper = new SearchHelper(db);

        //Get Machine Details
        var dbResults = searchHelper.SearchModal(Convert.ToInt32(machineId));

        machine.Model = dbResults.Model;
        machine.Catagory1 = dbResults.Catagory1;
        machine.Category2 = dbResults.Category2;
        machine.Manufacturer = dbResults.Manufacturer;
        machine.Region = dbResults.Region;
        machine.SpecDate = dbResults.SpecDate;
        machine.Status = dbResults.Status;
        machine.MachineId = dbResults.MachineId;
        machine.ManufacturerId = dbResults.ManufacturerId;

        var model = machine;

        return PartialView("_SearchModal", model);
    }
}

First thing I checked was the scripts, they're all in place when the layout page loads so it's not a script issue. Not sure what to change to even try at this point so any suggestions welcome.

In the ajax form:

_GetSearch => _GetSearchModal(name of the action)

Try to return machine to the partial view? Maybe see in the View hierarchy, is there is second _SearchModal partial view, that gets returned?

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