简体   繁体   English

jQuery Ajax调用锚标记单击+ MVC 4

[英]jquery ajax call on anchor tag click + mvc 4

I have a view into which I am rendering the folders exists in the given path. 我有一个视图,我正在渲染给定路径中存在的文件夹。

we hit the controller action with the root directory as parameter and the action will return the contents in root (folder names, file names , images names etc.,). 我们以根目录为参数命中控制器动作,该动作将返回根目录下的内容(文件夹名,文件名,图像名等)。 These details are rendered into a view with folders as anchors. 这些详细信息以文件夹作为锚点呈现到视图中。

Here the requirement is, on click on any folder in the view, we have to do an ajax request and fetch the contents of that folder and show it. 这里的要求是,单击视图中的任何文件夹,我们必须执行ajax请求并获取该文件夹的内容并显示它。 Here the problem is, to handle click event in jquery, I dont have ids to anchors. 这里的问题是,要处理jquery中的click事件,我没有锚定ID。 Even if I render ids to anchors, they are not going to be same always due to changing number of folders. 即使我将ID呈现给锚点,由于文件夹数量的变化,它们也不总是一样。

So how can I handle these ajax calls in jquery. 那么我该如何在jquery中处理这些ajax调用。

Is it ever possible Or do we need to make a normal request only click of an anchor? 有可能吗?还是只需要单击锚点就可以发出正常请求?

Please advise.. 请指教..

EDIT: my view 编辑:我的看法

@model IVM
@{

ImgFolder imgFolder = Model.ViewModel as ImgFolder;
string dirName, fileName;

}

@{Uri uri = Request.Url;}

@*Show the current directory name using page URL. *@
<h2>@Server.UrlDecode(uri.Segments.Last())</h2>


<ul>
    @*Listing the directories *@
    @foreach (var folder in imgFolder.Folders)
    {
        dirName = new DirectoryInfo(folder).Name;


        <li>

            <a class="imagefolder" title="@dirName">@dirName</a>


        </li>


    }

    @* Listing files *@
    @foreach (var file in imgFolder.Files)
    {
        fileName = Path.GetFileName(file);
        <li>
            <a href="@(uri.AbsolutePath + fileName)" title="@fileName" target="_blank">@fileName</a>
        </li>
    }
</ul>

add a class on root ul: 在根ul上添加一个类:

<ul class="directories">
    @*Listing the directories *@

and write jquery event like this: 并编写如下的jquery事件:

$(".directories a").click(function(e){

    e.preventDefault();

    var tagId = this.id // clicked anchor tag id

    var url = $(this).prop("href");

   // send ajax call here

   $.ajax ({


   });


});

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

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