简体   繁体   English

根据用户单击HTML MVC更改Url.Content

[英]Change Url.Content according to user click in HTML MVC

I have this piece of HTML page which creates a gallery and shows it. 我有一块HTML页面,它创建了一个画廊并显示了它。 Now the thing is, that i want to change the directory to take the photos from according to the user click. 现在的事情是,我想更改目录以根据用户单击来拍摄照片。 So, at first im creating a navbar where each folder is a button. 因此,首先,我在每个文件夹都是一个按钮的地方创建了一个导航栏。 Then, i have to catch the users click and place the directory name that he choose (@dir.name) in the Url.Content and in the Server.MapPath (replace the '???'). 然后,我必须捕获用户单击并将其选择的目录名称(@ dir.name)放在Url.Content和Server.MapPath中(替换'???')。 Any ideas about how to do that? 关于如何做到这一点的任何想法? Thanks in advance. 提前致谢。

 <div class="collapse navbar-collapse" id="myNavbar"> <ul class="nav navbar-nav"> @foreach (var dirPath in Directory.GetDirectories(Server.MapPath("~/Images/Customers/A"))) { var dir = new DirectoryInfo(dirPath); <li id="@dir.Name"><a href="#">@dir.Name</a></li> } </ul> </div> </div> </nav> <div class="preload"></div> <div class="gallery"> <div class="container-fluid"> <div class="row"> @foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/Images/Customers/A/???"), "*.jpg")) { var img = new FileInfo(imgPath); <div class="col-md-3 batas"> <a href="@Url.Content(String.Format("~/Images/Customers/A/???/{0}", img.Name))"><img src="@Url.Content(String.Format("~/Images/Customers/A/???/{0}", img.Name))" data-lity class="img-thumbnail" /></a> </div> } </div> </div> </div> 

Full page is here: https://github.com/ohadki/ShmuliksProject/blob/master/ShmuliksProject/Views/Home/CustomerGallery.cshtml 整页在这里: https : //github.com/ohadki/ShmuliksProject/blob/master/ShmuliksProject/Views/Home/CustomerGallery.cshtml

If you want to update the images when user clicks one of the folder name links, you need to execute some C# code which takes the currently clicked link (the directory name) and get the the files under that using the File.IO api's and use the file names to build the image tags. 如果要在用户单击文件夹名称链接之一时更新图像,则需要执行一些C#代码,该代码将使用当前单击的链接(目录名称),并使用File.IO api来获取位于该文件夹下的文件,并使用用于构建图像标签的文件名。

I will create a seperate action method which does that. 我将创建一个单独的操作方法来执行此操作。 This action method will accept the directory name (which user selected) and gets the files under that, get the file names and build path to the images 此操作方法将接受目录名称(用户选择的目录)并获取该目录下的文件,获取文件名并建立图像的路径

public ActionResult GetImages(string directory)
{
    var urlPath = Path.Combine(Url.Content("~/Images/Customers/A/"), directory);
    var pathDir = Server.MapPath(urlPath);
    var files = new List<string>();
    if(Directory.Exists(pathDir))
    {

        var fileNames= Directory.GetFiles(pathDir, "*.jpg")
                                .Select(g=>Path.GetFileName(g));
        var fileUrls=fileNames.Select(a => urlPath +"/"+ a);
        files.AddRange(fileUrls);
    }
    return PartialView("_Images", files);
}

You can see that our action method is passing a list of strings to the partial view, so let's create a partial view called _Images.cshtml , which is strongly typed to a list of strings and write some code to loop through this collection and render the image tags 您可以看到我们的action方法正在将字符串列表传递给局部视图,因此让我们创建一个名为_Images.cshtml的局部视图,该视图被强类型_Images.cshtml字符串列表,并编写一些代码来遍历此集合并呈现图片标签

@model List<string>
<div class="container-fluid">
    <div class="row">
        @foreach (var imgPath in Model)
        {
            <div class="col-md-3 batas">
                <a href="@imgPath">
                    <img src="@imgPath" data-lity class="img-thumbnail" />
                </a>
            </div>
        }
        </div>
</div>

With this code in place, you can try to access the action method by going to the url (with a directory name passed in the query string) and see whether it renders the images. 使用此代码后,您可以尝试通过访问url(在查询字符串中传递目录名称)来访问action方法,并查看它是否呈现图像。

yourSiteBaseUrl/yourControllerName/GetImages?directory=profile

Assuming profile is a directory name under "~/Images/Customers/A/ 假设profile"~/Images/Customers/A/

If this much code works, next step is to handle the click event on the links. 如果有这么多代码,下一步就是处理链接上的click事件。 We will wire up a click event on the a tags and stop the default behavior (navigation), get the needed information (the directory name) and make an ajax call to our action method which will return the partial view result we want and update the UI 我们将在a标签上关联一个click事件,并停止默认行为(导航),获取所需的信息(目录名称),并对我们的action方法进行ajax调用,该方法将返回所需的部分视图结果并更新UI

In the below code snippet, i am adding a data-img-url attribute to each of the a tags and it's value will be the relative path to the GetImages action method with the directory name in the query string. 在下面的代码片段中,我正在向每个a标签添加data-img-url属性,其值将是GetImages操作方法的相对路径,其中查询字符串中包含目录名称。

<div class="collapse navbar-collapse" id="myNavbar">
    <ul class="nav navbar-nav">
        @{ var path = Server.MapPath("~/Images/Customers/A"); }
        @foreach (var dirPath in Directory.GetDirectories(path))
        {
            var dir = new DirectoryInfo(dirPath);
            <li>
                 <a href="#" 
                    data-img-url="@Url.Action("GetImages",
                                  new { directory = dir.Name })">@dir.Name</a>
           </li>
        }
    </ul>
</div>
<div id="gallery" class="gallery"></div>

Now our UI markup is ready, let's write some jQuery code to wire up the click event and then make the ajax call. 现在我们的UI标记已经准备好了,让我们编写一些jQuery代码来连接click事件,然后进行ajax调用。 In the below code snippet, I am using the jQuery load method to make the ajax call and update the DOM 在下面的代码片段中,我正在使用jQuery load方法进行ajax调用并更新DOM。

<script>
    $(function () {
        $("a[data-img-url]").click(function (e) {
            e.preventDefault();
            var url = $(this).data("img-url");
            $("#gallery").load(url);
        })
    })

</script>

This is the basic idea. 这是基本思想。 You can make improvements to the code snippets to make it more robust (null check, string concatenation etc) 您可以对代码段进行改进,使其更健壮(空检查,字符串串联等)

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

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