Solved

Ajax and MVC

Posted on 2016-07-25
2
62 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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now