Solved

Ajax and MVC

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP - AJAX and MySQL it works only if the value is a number 12 57
Code works but it's slow 28 71
imap mails 1 24
Client Validating 2 date fields, required & comparison 1 28
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

822 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