Solved

Ajax and MVC

Posted on 2016-07-25
2
96 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

Industry Leaders: 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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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