如何在MVC 5剃须刀中调用和刷新局部视图? [英] How to call and refresh a partial view in MVC 5 razor?

查看:84
本文介绍了如何在MVC 5剃须刀中调用和刷新局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户从下拉列表中的值中选择一个值时,如何用新数据刷新必须为空的创建视图上的局部视图?

How can a partial view on the create view that has to come in empty be refreshed with new data when a user selects a value from a list a values drop down?

我在留言板上以及其他板上都尝试了一堆东西,它只是深入挖掘了更多错误.我只是简直不敢相信,在Web表单中这么简单的事情在MVC中很难做到.

I have a tried a bunch of stuff listed on the message board as well as others and it just drills down to more errors. I just can't believe something so easy in webforms is this hard to do in MVC.

我在Chrome和IE中都尝试了此操作,但出现错误,所以我迷路了.对于共享文件夹中的部分视图,我有类似的内容:

I tried this both in Chrome and IE and get errors so I'm lost. I have something like this for a partial view in the shared folder:

<table cellpadding="1" border="1">
    .... // table header
    @foreach (SYSTEMX.Models.VUE_ISSUE_LIST item in ViewBag.IssuesList)
    {
        <tr>
           <td>@item.Issue</td>
       </tr>
    }
</table>

创建cshtml文件具有以下内容:

The Create cshtml file has this:

<div class="col-sm-6">
    <div class="form-horizontal" style="display:none" id="PV_IssueList">
        @{ Html.RenderAction("UpdateIssuesList"); }
    </div>
</div>

在Controller中有与此类似的代码

In the Controller there is code similar to this

[HttpGet]
public ActionResult UpdateIssuesList(int? VID)
{
    ViewBag.IssuesList = GetIssuesList(VID);
    return PartialView("UpdateIssuesList");
}

GetIssuesList(VID)看起来与此非常相似,它位于mvc应用程序的控制器中

The GetIssuesList(VID) looks very similar to this and it is in the controller of the mvc application

private List<VUE_ISSUE_LIST> GetIssuesList(int? VID)
{
    return db.VUE_ISSUE_LIST_.Where(i => i.ID == VID).ToList();
}

我收到此错误.我对这里发生的事情一无所知.

I get this error. I'm clueless to what is going on here.

找不到部分视图'UpdateIssuesList',或者没有视图引擎支持搜索到的位置.搜索了以下位置: 〜/Views/CONTROLLERX/UpdateIssuesList.aspx
〜/Views/CONTROLLERX/UpdateIssuesList.ascx
〜/Views/Shared/UpdateIssuesList.aspx
〜/Views/Shared/UpdateIssuesList.ascx
〜/Views/CONTROLLERX/UpdateIssuesList.cshtml
〜/Views/CONTROLLERX/UpdateIssuesList.vbhtml
〜/Views/Shared/UpdateIssuesList.cshtml
〜/Views/Shared/UpdateIssuesList.vbhtml

The partial view 'UpdateIssuesList' was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/CONTROLLERX/UpdateIssuesList.aspx
~/Views/CONTROLLERX/UpdateIssuesList.ascx
~/Views/Shared/UpdateIssuesList.aspx
~/Views/Shared/UpdateIssuesList.ascx
~/Views/CONTROLLERX/UpdateIssuesList.cshtml
~/Views/CONTROLLERX/UpdateIssuesList.vbhtml
~/Views/Shared/UpdateIssuesList.cshtml
~/Views/Shared/UpdateIssuesList.vbhtml

一个论坛用户发布了这样的解决方案,我猜这对他有用,因为他贴了一个绿色的复选标记,但对我却根本不起作用:

A forum user posted something like this as a solution and I guess it worked for him as he put a green check mark but it does not work for me at all:

更新PartialView mvc 4

也尝试过这个:

刷新mvc中的部分视图

也请仔细阅读,但技术性太强,以至于我不太了解.

Also read over this but it is so very technical that I don't follow it all that well.

https://www.simple-talk.com/dotnet/asp-net/tips-and-tricks-about-razor-partial-views/

推荐答案

我最终在后面的代码中使用了一些称为Ajax的代码,该代码从下拉列表中读取了点击.获取所选项目的值,并将值传递回

I ended up using some called Ajax in the code behind that reads the click from the dropdown list. get the value of the selected item and passed the values back to

后面的所有控制器代码均用于构建列表,并将其发送以更新部分视图,如果有数据,它将通过部分视图

all of the controller code behind to build the list and send it to update the partial view and if there is data there it pass the partial view

,其中包含创建表单的更新列表.

with the update list to the create form.

    $(document).ready(function () {
        $('#RES_VID').change(function ()
        {

            debugger;

            $.ajax(

                {
                    url: '@Url.Action("UpdatePartialViewList")',
                    type: 'GET',
                    data: { VID: $('#RES_VID').val() },

                    success: function (partialView)
                    {
                        $('#PV_WidgetList').html(partialView);
                        $('#PV_WidgetList').show();
                    }
                });

这篇关于如何在MVC 5剃须刀中调用和刷新局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆