简体   繁体   English

MVC-返回的部分视图正在创建一个新页面

[英]MVC - Returning partial view is creating a new page

I have a form with some input fields. 我有一些输入字段的表单。 When a user clicks on the submit I want to post the fields back, use them to query a service and load some results into a view on the same page while preserving the original input. 当用户单击“提交”时,我想将字段回发,使用它们查询服务并将某些结果加载到同一页面的视图中,同时保留原始输入。 For this, I've scaffolded a view using my model and added a partial view below to populate the results with. 为此,我使用模型搭建了一个视图,并在下面添加了一个局部视图以填充结果。

....bottom of form <div class="form-group">
        @Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PostalAmount, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Search" class="btn btn-default" />
        </div>
    </div>
</div>

<div class="list-group">
    @Html.Partial("_ResultsPartial")
</div>

In my controller, I have 2 actions, one to create the initial view and one for the post back. 在控制器中,我有2个动作,一个用于创建初始视图,另一个用于回发。

        [HttpGet]
    public ActionResult Search()
    {
        ViewBag.Message = "Enter search details";

        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public PartialViewResult Search(SearchCriteria searchCriteria)
    {
        List<SearchResult> results = new List<SearchResult>();
        if (ModelState.IsValid)
        {
           //await go off and do the actaul search
           results = _dataProvider.GetData(searchCriteria);
        }
        return PartialView("~/Views/Shared/_ResultsPartial.cshtml", results);
    }

So in order to render the partial view, I use return PartialView passing in the new model which contains the results of my search. 因此,为了呈现部分视图,我使用return PartialView传入包含搜索结果的新模型。 One of the problems I'm having with this is that creates a new page, ie the partial view doesn't load on the existing page. 我遇到的问题之一是创建一个新页面,即部分视图未加载到现有页面上。 What other way is there to render a partial view while in a controller action so that the partial view loads on the page it was created? 还有什么其他方法可以在执行控制器操作时呈现部分视图,以便将部分视图加载到创建它的页面上?

尝试用以下方式替换渲染:

@{Html.RenderAction("Results", new {results = model.Results})};

试试这个

return PartialView("_ResultsPartial", results);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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