Solved

Ajax and MVC

Posted on 2016-07-25
2
89 Views
Last Modified: 2016-09-16
Hi, I am a beginner to Ajax and MVC, and am trying to figure out how to get a particular thing to work

Basically, I have a method that gets all directories/subdirectories from a given root directory, and what I want to do is clicking on a directory to have it populate a span/div directly below it with all the files within that directory

I'll show you what I've got right now, and hopefully you'll be able to tell me what I'm missing/doing wrong.  Thanks a lot for your time

Controller:
public PartialViewResult GetFiles(string path)
        {
            ImageFileModel[] files = OriginationViewerModelBuilder.GetImageFiles(path);
            return PartialView(files);
        }

Open in new window


View:
@helper ShowDirectoryTree(ImageDirectoryModel imageDirectory)
{
    <ul>
        <li>
            <b>@Ajax.ActionLink(imageDirectory.Path,"GetFiles",new {path = imageDirectory.Path},new AjaxOptions{UpdateTargetId=imageDirectory.FileSpanID})</b>
            <span id="@imageDirectory.FileSpanID"></span>
        </li>
        
        @if (imageDirectory.ImageSubDirectories.Any())
        {
            foreach (var sub in imageDirectory.ImageSubDirectories)
            {
                @ShowDirectoryTree(sub);
            }
        }

    </ul>
}


@Model.Title

Image Folder: @Model.Configuration.Options.OriginalImagesFolder
<br />
@foreach (var directory in Model.ImageDirectories)
{
    @ShowDirectoryTree(directory);
}

Open in new window


Partial View GetFiles:
@model IEnumerable<ImageFileModel>

<ul>
    @foreach (var file in Model.ToArray())
    {
        <li>@file.Filename</li>
    }
</ul>

Open in new window


Now I think I'm just missing some javascript, or am mis-using the Ajax Action link, as currently clicking on the link will just open up the partial view as a new page, but not quite sure how to pull it all together
0
Comment
Question by:krapvag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
Steven Kribbe earned 500 total points
ID: 41801039
I think  the target id of the ajac action link should be the EVALUATION of the fileSpanID and not the literal string, and I think it need to be in quotes:

UpdateTargetId="@imageDirectory.FileSpanID"
0
 
LVL 1

Author Comment

by:krapvag
ID: 41801114
I see, thanks a lot for the response.  I forgot about this question as I had to go non-ajax in the end but your response makes sense
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question