代码之家  ›  专栏  ›  技术社区  ›  JL1

MVC-加载PartialView刷新整个页面

  •  0
  • JL1  · 技术社区  · 2 年前

    在核心,我试图生成一个带有链接的表,这些链接可以在重新加载PartialView时单击。一切正常,只是我无法停止刷新页面。我想做的只是在researchDiv中加载PartialView。我确实有 <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 在web.config中。

    我已经将视图和局部视图更改为现在使用ajax,但同样的问题也在发生,我不完全确定该怎么办。如果我单击一个链接,整个页面都会刷新,而不仅仅是htmlContainer。

    在我的_LayoutDashboard中,我确实有不引人注目的jscripts,当页面呈现时,有0个控制台错误。

    <script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript">
    

    这是视图

    @model Stocks.BLL.ExecutingTrades.Model.WatchList.ExeTradesWatchList
    
    @{
        ViewBag.Title = "WatchList";
        Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
    }
    
    <style>
        #customers {
            font-family: Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 20%;
            border: 1px solid black;
        }
    
            #customers td, #customers th {
                border: 1px solid #ddd;
                padding: 8px;
            }
    
            #customers tr:nth-child(even) {
                background-color: #f2f2f2;
            }
    
            #customers tr:hover {
                background-color: #ddd;
            }
    
            #customers th {
                padding-top: 12px;
                padding-bottom: 12px;
                text-align: left;
                background-color: #04AA6D;
                color: white;
            }
    
        .float-container {
            border: 3px solid #fff;
            padding: 20px;
        }
    
        .float-childsmall {
            float: left;
        }
    
        .float-child {
            width: 80%;
            float: left;
            height: 1000px;
        }
    </style>
    <script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
    <p>Last Update: @Html.Label(DateTime.UtcNow.ToString()) - Time in UTC</p>
    
    <div class="float-container">
    
        <div class="float-childsmall">
            @foreach (var watchList in Model.ViewExecutingTradesUserWatchListFollowShort)
            {
                <table id="customers">
                    <caption class="text-center"><h2>@watchList.WatchListName</h2></caption>
                    <caption class="text-center"><p style="font:10px;">@watchList.WatchListDescription</p></caption>
                    <tr>
                        <th>Ticker</th>
                    </tr>
                    @foreach (var ticker in Model.ViewUserWatchListTickerModel.Where(y => y.UserWatchListId == watchList.UserWatchListId).ToList())
                    {
                        <tr>
                            <td><a target="_blank" href="https://finviz.com/quote.ashx?t=@ticker.Ticker">@ticker.Ticker </a></td>
                            <!--<td>
                                @{
                                    <button type="button" class="btn" id='wlButton_@watchList.WatchListName+@watchList.UserWatchListId+@ticker.Ticker'>
                                        @Html.DisplayFor(modelItem => @ticker.Ticker)
                                    </button>
                                }
                            </td>-->
                            <div class="btnB" id='div_@watchList.WatchListName+@watchList.UserWatchListId+@ticker.Ticker'>
                                <p class="category-title">@ticker.Ticker</p>
                            </div>
                        </tr>
                    }
                </table>
            }
        </div>
        <div class="float-child" id="researchDiv">
            <div id="htmlContainer">
                @Html.Partial("_Research", new ExecutingTrades.Models.TickerInMem() { Ticker = "META" });
            </div>
        </div>
    </div>
    <script>
        $(document).ready(function () {
            $(".btnB").click(function () {
                $.ajax({
                    url: '@Url.Action("_Research", "Dashboard")',
                    type: "GET",
                    data: { ticker: this.id.substring(this.id.lastIndexOf('+')) },
                    cache: false,
                    async: true,
                    success: function (data) {
                        $("#htmlContainer").html(data);
                    }
                });
            });
        })
    </script>
    

    _研究是局部的

    @model ExecutingTrades.Models.TickerInMem
    <!-- TradingView Widget BEGIN -->
    <style>
        .tradingview-widget-container {
            border: 3px solid #fff;
            padding: 20px;
            height: 650px;
        }
    </style>
    <div class="tradingview-widget-container">
    
        <div class="tradingview-widget-copyright"><a href="https://www.tradingview.com/symbols/@Model.Exchange-@Model.Ticker/" rel="noopener" target="_blank"><span class="blue-text">@Model.Ticker</span></a> by TradingView</div>
        <script type="text/javascript">
            alert(@Model.Ticker);
        </script>
        <script type="text/javascript">
            new TradingView.widget(
                {
                    "autosize": true,
                    "symbol": "NASDAQ:@Model.Ticker",
                    "interval": "D",
                    "timezone": "Etc/UTC",
                    "theme": "light",
                    "style": "1",
                    "locale": "en",
                    "toolbar_bg": "#f1f3f6",
                    "enable_publishing": false,
                    "allow_symbol_change": true,
                    "container_id": "tradingview_37810"
                }
            );
        </script>
    </div>
    

    以及控制器

    public ActionResult _Research(string ticker)
        {
            if (string.IsNullOrWhiteSpace(ticker))
                return View();
    
            var model = new TickerInMem();
            model.Ticker = ticker.Split('+').Last();
            model.Exchange = "NASDAQ";
            return PartialView("_Research", model);
        }
    

    当我调试一切正常时,我可以调用部分视图,并且模型是正确的。

    页面加载时: enter image description here

    单击链接时: enter image description here

    我真正想做的就是加载第二个图形,我将其单击到视图上渲染部分视图的位置。

    0 回复  |  直到 2 年前
        1
  •  0
  •   JL1    2 年前

    所以我的MVC和Ajax是正确的,真正的问题是在tradingview控件内部,我删除了它在容器中引用的div。 “container_id”:“tradingview_37810”