How to call and refresh a partial view in MVC 5 ra

2019-05-31 23:23发布

问题:

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?

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.

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>

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>

In the Controller there is code similar to this

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

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.

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:

Updating PartialView mvc 4

Also tried this:

refresh a partial view in 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/

回答1:

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();
                    }
                });


回答2:

Is that enough?

$("#PV_IssueList").load("/controllerx/UpdateIssuesList?VID=1");

When you need refresh, call this jquery line... or put it inside a function, or any event.. for example onclick='$("#PV_IssueList").load("/controllerx/UpdateIssuesList?VID=1");'