【问题标题】:PartialView not returned in ModalPartialView 未在 Modal 中返回
【发布时间】:2021-04-21 06:27:23
【问题描述】:

我想在模式窗口中返回一个视图。我已经通过 ajax 调用在不同的站点上完成了它,但是这次不返回视图。这是 ajax 调用:

function fill() {
var ISIN = document.getElementById('ISIN').value
if (ISIN != null) {
    
        $.ajax({
            type: "POST",
            url: "/Order/FillFormWithISIN",
            data: {ISIN: ISIN},
            success: function (data) {
                $('.modal-body').html(data);
                $("#myModal").modal("show");
            },
            error: function () {
                $('.modal-body').html(data);
                $("#myModal").modal("show");
            }
        });
    }
    }

Controller-function调用正确,所以url一定是正确的。

这是控制器功能:

public IActionResult FillFormWithISIN (string ISIN)
        {
            Instrument instrument = new Instrument { ISIN = ISIN};
            instrument.GetDetails();
            return PartialView("~/Views/Shared/multipleResultsPartial.cshtml", instrument); 
        }

当我调试调试器时,只是跳过“返回 PartialView”而不做任何事情。

这是我的部分视图:

    @model Instrument


@if (Model.Response.GetInstrumentInfosResponse1.Length > 0)
{
    @foreach (var inst in Model.Response.GetInstrumentInfosResponse1)
    {
        <div class="col-md-8">
            <p>@inst.Name</p> 
        <p>@inst.Ccy</p>
        </div>
    }
}
else { 
<p>An instrument with the given ISIN could not be found. Do you want to create an instrument?</p>
}

PartialView 的 url 也是正确的,我已经检查过了。

这是我希望返回结果的模态窗口:

<div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog modal-lg">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" onclick="javascript:window.location.reload()" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Save Status</h4>
                </div>
                <div class="modal-body">
                    <div class="cv-spinner">
                        <span class="spinner"></span>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" onclick="javascript:window.location.reload()" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

我这样调用函数:

<div class="col-md-2">
                <form>
                    <div class="form-group">
                        <label asp-for="ISIN" class="control-label">ISIN</label>
                        <input asp-for="ISIN" id="ISIN" class="form-control" value="@Model.ISIN" />
                        <span asp-validation-for="ISIN" class="text-danger"></span>
                        <button class="btn" onclick="fill()" id="searchButton"  disabled style="float:right; pointer-events: none;cursor: default">Search</button>
                    </div>
                </form>
            </div>

控制台窗口的输出是这样的:

    Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executing action method NXM_Web_Client.Controllers.OrderController.FillFormWithISIN (NXM_Web_Client) - Validation state: Valid
"dotnet.exe" (CoreCLR: clrhost): "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Remote Debugger\x64\Runtime\Microsoft.VisualStudio.Debugger.Runtime.NetCoreApp.dll" geladen. 
Ausnahme ausgelöst: "System.IO.FileNotFoundException" in System.Private.CoreLib.dll
"dotnet.exe" (CoreCLR: clrhost): "Microsoft.GeneratedCode" geladen. 
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action method NXM_Web_Client.Controllers.OrderController.FillFormWithISIN (NXM_Web_Client), returned result Microsoft.AspNetCore.Mvc.PartialViewResult in 7301.4469ms.
Microsoft.AspNetCore.Mvc.ViewFeatures.PartialViewResultExecutor:Information: Executing PartialViewResult, running view ~/Views/Shared/multipleResultsPartial.cshtml

所以PartialView实际上是被调用了,但是没有显示在modal中。

我尝试在'instrument.GetDetails()'-Part 中添加注释以检查是否返回了视图,但仍然没有返回。所以问题不存在。 有时当我调试时,我会在 jQuery.js 的一部分代码中使用以下注释: /

/ Discard the second event of a jQuery.event.trigger() and
                // when an event is called after a page has unloaded

也许这与问题有关。

这是仪器型号:

    namespace NXM_Web_Client.Models
{
    public class Instrument
    {
        public GetInstrumentInfosResponse Response { get; set; }
        public ExtractInstrumentResponse Response2 { get; set; }
        public string InstName { get; set; }
        public long InstID { get; set; }
        public string InstAcr { get; set; }
        public long SecurityCode { get; set; }
        public string SecurityName { get; set; }
        public string ISIN { get; set; }

        /// <summary>
        /// Function that retrieves Information about the Instruments' Details from the Instrument-Webservice
        /// </summary>
        public void GetDetails()
        {
            InstrumentService_v1_0Client client = new InstrumentService_v1_0Client();
            client.ClientCredentials.UserName.UserName = "B019438";
            client.ClientCredentials.UserName.Password = "PASSWORD";
            client.Endpoint.EndpointBehaviors.Add(new SigmaLoggerMessageInspector());
            SoapSecurityHeader securityHeader = new SoapSecurityHeader("B019438", "PASSWORD");
            _ = new InstrumentInfoSearchCriteria();
            InstrumentInfoSearchCriteria searchCriteria;
            if (InstID != 0)
            {
                searchCriteria = new InstrumentInfoSearchCriteria()
                {
                    ISIN = ISIN,
                    SecurityCode = SecurityName,
                    SecurityType = SecurityCode,
                    ID = InstID,
                    IDSpecified = true,
                    Name = InstName
                };
            }
            else
            {
                searchCriteria = new InstrumentInfoSearchCriteria()
                {
                    ISIN = ISIN,
                    SecurityCode = SecurityName,
                    SecurityType = SecurityCode,
                    Name = InstName
                };
            }


            GetInstrumentInfos getInstrumentInfos = new GetInstrumentInfos()
            {
                InstrmtInfoCrit = searchCriteria
            };

            try
            {
                using (new OperationContextScope(client.InnerChannel))
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(securityHeader);
                    Response = client.GetInstrumentInfosAsync(getInstrumentInfos).GetAwaiter().GetResult();

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Function that extracts more specific Instrument Information about the Instruments from the Instrument-WebService.
        /// For example when calling the Details-View in the Portfolio.
        /// </summary>
        public void Extract()
        {
            InstrumentService_v1_0Client client = new InstrumentService_v1_0Client();
            client.ClientCredentials.UserName.UserName = "B019438";
            client.ClientCredentials.UserName.Password = "PASSWORD";
            client.Endpoint.EndpointBehaviors.Add(new SigmaLoggerMessageInspector());
            SoapSecurityHeader securityHeader = new SoapSecurityHeader("B019438", "PASSWORD");

            InstrumentInfoSearchCriteria instrumentInfoSearch = new InstrumentInfoSearchCriteria()
            {
                ID = InstID,
                Name = InstName,
                Acronym = InstAcr,
                SearchMode = SearchMode.EQ

            };

            InstrumentInfoSearchCriteria[] instrumentInfoSearchCriterias = new InstrumentInfoSearchCriteria[]
                    {
                        instrumentInfoSearch
                    };

            Filter filter = new Filter()
            {
                Instrument = instrumentInfoSearchCriterias
            };

            ExtractInstrument extractInstrument = new ExtractInstrument()
            {
                Filter = filter
            };
            using (new OperationContextScope(client.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(securityHeader);
                Response2 = client.ExtractInstrumentAsync(extractInstrument).GetAwaiter().GetResult();

            }
        }

        /// <summary>
        /// Function that retrieves the FIGI-Code from the FIGI-Webservice. (not done yet)
        /// </summary>
        public void GetFIGI()
        {
            string ID = "B025228-" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss"); //user in Config-file
            string version = "0.1"; //version in Config-File
            OpenFIGIServiceClient client = new OpenFIGIServiceClient();
            client.ClientCredentials.UserName.UserName = "B025228";
            client.ClientCredentials.UserName.Password = "hjH#1UL4";
            client.Endpoint.EndpointBehaviors.Add(new SigmaLoggerMessageInspector());
            getOpenFIGI openfigi = new getOpenFIGI
            {
                ISIN = this.Response.GetInstrumentInfosResponse1[0].ISIN
            };
            ESBHeader header = new ESBHeader
            {
                trackingID = ID,
                version = version
            };
            try
            {
                getOpenFIGIRequest request = new getOpenFIGIRequest { ESBHeader = header, getOpenFIGI = openfigi };
                using (new OperationContextScope(client.InnerChannel))
                {
                    getOpenFIGIResponse1 figi = client.getOpenFIGIAsync(header, openfigi).Result;
                }

            }
            catch (Exception)
            {
                //throw (e);
            }

        }

    }
}

我知道该网站上的模态必须正常工作,因为我还在单击该网站的另一个按钮上调用了模态,并且它可以正常工作。我不知道为什么这次它不起作用。 感谢您的帮助。

【问题讨论】:

  • 同一页面中是否有多个模式?
  • 不,我只有一个模态。我在不同的按钮上调用了相同的模态,显示不同的信息(来自不同控制器的结果)。
  • 这条线是做什么的instrument.GetDetails();你说的是just steps over the 'return PartialView' ,可能这个函数有错误。
  • instrument.GetDetails 调用 Web 服务以根据其 ISIN 获取工具的详细信息。它有效,其他功能也在使用它,我在调试器中看到该功能的结果是正确的。但我现在仍然会研究它。
  • 我只是在注释掉这部分的时候尝试了这个功能,它仍然不起作用,所以问题很可能不存在。

标签: javascript ajax asp.net-core-mvc partial-views


【解决方案1】:

您必须指定您的期望。在这种情况下是 html。

    $.ajax({
        type: "POST",
        url: "/Order/FillFormWithISIN",
        dataType: 'html',
        data: {ISIN: ISIN},
        success: function (data) {
            $('.modal-body').html(data);
            $("#myModal").modal("show");
        },
        error: function () {
            $('.modal-body').html(data);
            $("#myModal").modal("show");
        }
    });

添加更多详细信息后,您的局部视图似乎有问题。控制器返回错误。你可以做这样的事情来确定细节

    public IActionResult FillFormWithISIN(string ISIN)
    {
        try
        {
            Instrument instrument = new Instrument { ISIN = ISIN };
            instrument.GetDetails();
            return PartialView("~/Views/Shared/multipleResultsPartial.cshtml", instrument);
        }
        catch Exception(ex)
        {
            //your method to log exception
        }

    }

【讨论】:

  • 控制器真的返回视图吗?
  • 这个Controller类中的其他函数除了这个之外都工作正常,所以我猜它正常返回视图。但是这个函数不返回视图。
  • 检查成功:function console.log(data) 你在控制台看到数据了吗?
  • 我检查了输出并将其添加到问题描述中,我没有看到仅实际调用了 Partial View 的数据。
  • 您的控制器实际上返回了错误。这意味着您的局部视图有问题。
猜你喜欢
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 2015-03-11
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
相关资源
最近更新 更多