简体   繁体   English

在MVC中部分加载时如何停止刷新视图?

[英]How to stop view to refresh when partial loads in mvc?

When I click on Partial View element on tab click like India then the whole view is getting refreshed. 当我单击tab上的“局部视图”元素时,如“ India则整个视图将得到刷新。 I tried using Ajax but control does not even hit there. 我尝试使用Ajax但控件甚至都没有打到那里。 Where I am doing wrong, can anyone please guide me? 我在哪里做错了,谁能指导我?

Controller 调节器

public ActionResult Index()
{
    ClsHome model = new ClsHome();

    return View(model);
}

public PartialViewResult EmployeeBasedCountry(int CountryId)
{
    ClsHome clshome = new ClsHome();
    clshome.Country = CountryId;

    clshome.countries = CountryFilter(CountryId);          
    return PartialView("~/Views/Home/_pEmp.cshtml", clshome);
}

View 视图

@model EmpWebsite.Models.Home.ClsHome      

<div id=Partial class="col-md-5">                 
   @Html.Partial("_pEmp")
</div>

Partial 局部

@model EmpWebsite.Models.Home.ClsHome  
<ul class="nav nav-tabs nav-justified">
    <li class="active">@Html.ActionLink("India", "EmployeeBasedCountry", "Home", new {@class = "ActionLinkId", @CountryId = "1" },null)
    </li> 
    <li>@Html.ActionLink("USA", "EmployeeBasedCountry", "Home", new {@class = "ActionLinkId", @CountryId = "2" },null)</li>       
</ul>

<div class="tab-content">
    <div class="panel">
        <table class="table-striped">
            <tr class="heading">
                <th>
                    EmpId
                </th>
                <th>
                    EmpName
                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @item.EmpId
                    </td>
                    <td>
                        <a>@item.EmpName</a>
                    </td>
                </tr>
            }
        </table>
    </div>
</div>

Script 脚本

 $(document).ready(function () {
            $('.ActionLinkId').on("click", function () {
                    debugger;
                    $.ajax({
                       url: '@Url.Action("EmployeeBasedCountry", "Home")',
                        type: "GET",
                        data: { data }
                    })
                        .done(function (partialViewResult) {
                            debugger;
                            $("#Partial").html(partialViewResult);
                        });
                });
            });

You have your parameters around the wrong way for @Html.ActionLink 您为@Html.ActionLink了错误的参数

@Html.ActionLink("USA", "EmployeeBasedCountry", "Home", 
                 new { @class = "ActionLinkId", @CountryId = "2" }, 
                 null)

this overload is: linktext, action, controller, routeValues, htmlAttributes where htmlAttributes is null so your rendered <a> does not have these attributes. 此重载是: linktext, action, controller, routeValues, htmlAttributes ,其中htmlAttributes为null因此呈现的<a>没有这些属性。

Easy enough to check your rendered html which will be like: 很容易检查您呈现的 html,如下所示:

<a href="/Home/EmployeeBasedCountry?class=ActionLinkId&countryId=2">USA</a>

So your link doesn't have the .ActionLinkId class, so the jquery doesn't fire. 因此,您的链接没有.ActionLinkId类,因此不会触发jquery。 Also easy enough to check with: 也很容易检查:

$(function() { alert($(".ActionLinkId").length) }); 

(or use browser debugger earlier ) (或更早使用浏览器调试器)

Fix is to remove the null at the end: 解决的方法是在最后删除null

@Html.ActionLink("USA", "EmployeeBasedCountry", "Home", new {@class = "ActionLinkId", @CountryId = "2" })

or split them, depending on the requirement 或拆分它们,具体取决于要求

@Html.ActionLink("USA", "EmployeeBasedCountry", "Home", new { @CountryId = "2" }, new {@class = "ActionLinkId" })

When you're clicking the link, the ajax request sent, but the link do its action, too, and refrsh the page. 当您单击链接时,发送了ajax请求,但该链接也执行其操作,并刷新了页面。 So, you should prevent the browser from refrsh the page (For more information see MDN Docs ): 因此,您应该防止浏览器刷新页面(有关更多信息,请参见MDN Docs ):

$('.ActionLinkId').on("click", function(e) {
  e = e || window.event; // To support old browsers
  e.preventDefault();
  $.ajax({
    url: '@Url.Action("TabLeadingSires", "Home")',
    type: "GET",
    data: { data }
  }).done(function(partialViewResult) {
    $("#Partial").html(partialViewResult);
  });
});

You are using 您正在使用

   @Html.ActionLink("India", "EmployeeBasedCountry", "Home", new {@class = "ActionLinkId", @CountryId = "1" }

which is used to load page... just use class or id parameter to invoke ajax. 用于加载页面...只需使用class或id参数来调用ajax。

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

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