为什么$ .ajax在我的项目asp.net mvc 4中不起作用 [英] why $.ajax not work in my project asp.net mvc 4

查看:59
本文介绍了为什么$ .ajax在我的项目asp.net mvc 4中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
这是我的行动:

  public  ActionResult Index( int  page =  1 
{
var model =( from item in _db.News
join c _db.SPNews on item.Id等于c.NewsId
orderby item.CreateDate
其中 c.GroupNewsId == 1
选择项目。).ToPagedList(页面, 6 );
if (Request.IsAjaxRequest())
{
return PartialView( _ AllNewsView,model);
}
返回查看(模型);
}





这是视图:

 @model IPagedList <  新闻 >  
@ {
ViewBag.Title =خبرها;
}

< h2 > آرشیوکاملخبرها< / h2 >
@ Html.Partial(_ AllNewsView,型号)





this部分视图:

 @ model IPagedList <  新闻 >  
< div id = ViewAllNews >
< div id = 投资组合 >
< ul >
@ {int count = 0;}
@foreach(模型中的var项)
{
if(count%2 == 0)
{
< li >

< img src = @ Url.Content(item.ImageUrl) alt = / >
< h2 > @ item.Title < / h2 >
< p > @item。摘要< / p >
< p class = readmore > < a href = > 在此处阅读更多»< / a > < / p >
< / li >
}
其他
{
< li class = last >
< img src = @ Url.Content(item.ImageUrl) alt = / > ;
< h2 > @ item.Title < / h2 >
< p > @ item.Summary < / p >
< p class = readmore > < a href = > 在此处阅读更多内容»< / a > < / p >
< / li >
}
count ++;
}

< / ul >
< / div >
< div class = Pagelist data-otf-target = #ViewAllNews >
@ Html.PagedListPager( Model,page => Url.Action(index,new {page}),
PagedList.Mvc.PagedListRenderOptions.ClassicPlusFirstAndLast)
< / div >

< / div >





这是我在otf js中的代码:

 $(function(){

var getpage = function(){

var $ a = $(本);

var options = {
url:$ a.attr(href),
type:GET
};
$ .ajax(options).done(function(data){
var target = $ a.parents(div)。parents(div)。attr(data-otf-target );
$(目标).replaceWith(数据);

});
返回false;
};
$(。pagination a)。click(getpage);
});





这是Bundleconfig:

 bundles.Add( new  ScriptBundle( 〜/ bundles / jquery)。包含(
〜/ Scripts / jquery- {version} .js
〜/ Scripts / jquery-slider.js
〜/ Scripts / jquery-custom.js
〜/ Scripts / jquery-tabs.js
〜/ Scripts / jquery-prettyPhoto.js
〜/ Scripts / jquery.unobtrusive *
〜/ Scripts / otf.js
));





但是当我点击我页面中的标签时$ .ajax(选项)不起作用

解决方案

(function(){

var getpage = function(){

var

a =


(this);

var options = {
url:


hi this is my action :

public ActionResult Index(int page=1)
       {
           var model = (from item in _db.News
                        join c in _db.SPNews on item.Id equals c.NewsId
                        orderby item.CreateDate
                        where c.GroupNewsId == 1
                        select item).ToPagedList(page, 6);
           if(Request.IsAjaxRequest())
           {
               return PartialView("_AllNewsView", model);
           }
           return View(model);
       }



this is view :

@model IPagedList<News>
@{
    ViewBag.Title = "خبرها";
}

<h2>آرشیو کامل خبرها</h2>
@Html.Partial("_AllNewsView", Model)



this is partial view :

@model IPagedList<News>
<div id="ViewAllNews">
    <div id="portfolio">
        <ul>
            @{int count = 0;}
            @foreach (var item in Model)
            {
                if (count % 2 == 0)
                {
                    <li>

                        <img src="@Url.Content(item.ImageUrl)" alt="" />
                        <h2> @item.Title </h2>
                        <p>@item.Summary</p>
                        <p class="readmore"><a href="#">Read More Here »</a></p>
                    </li>
                }
                else
                {
                    <li class="last">
                        <img src="@Url.Content(item.ImageUrl)" alt="" />
                        <h2>@item.Title</h2>
                        <p>@item.Summary</p>
                        <p class="readmore"><a href="#">Read More Here »</a></p>
                    </li>
                }
                count++;
            }

        </ul>
    </div>
    <div class="Pagelist" data-otf-target="#ViewAllNews">
        @Html.PagedListPager(Model, page => Url.Action("index", new { page }),
        PagedList.Mvc.PagedListRenderOptions.ClassicPlusFirstAndLast)
    </div>

</div>



this my code in otf js:

$(function () {

    var getpage = function () {

        var $a = $(this);

        var options = {
            url: $a.attr("href"),
            type: "GET"
        };
        $.ajax(options).done(function (data) {
            var target = $a.parents("div").parents("div").attr("data-otf-target");
            $(target).replaceWith(data);
           
        });
        return false;
    };
    $(".pagination a").click(getpage);
});



this is Bundleconfig:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                      "~/Scripts/jquery-{version}.js",
                      "~/Scripts/jquery-slider.js",
                      "~/Scripts/jquery-custom.js",
                      "~/Scripts/jquery-tabs.js",
                      "~/Scripts/jquery-prettyPhoto.js",
                      "~/Scripts/jquery.unobtrusive*",
                      "~/Scripts/otf.js"
                     ));



but $.ajax(options) not work when i click on tag in my page

解决方案

(function () { var getpage = function () { var


a =


(this); var options = { url:


这篇关于为什么$ .ajax在我的项目asp.net mvc 4中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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