Good afternoon friends. I want to know how to correctly solve the following problem:

On the page there are two blocks: one conditionally "folders", the second part conditionally "files". Folder navigation is performed using Ajax and this part is implemented. Where there is a file there is a button "send to the current folder" (the one that is selected and opened now).

Question: how to get \ pass to the MoveToFolder(int ID_Folder, int ID_File) method MoveToFolder(int ID_Folder, int ID_File) . Variables called to facilitate understanding of the situation, the project is different, of course. ID_File is, and how to get ID_Folder , if navigation is performed dynamically without reloading the page?

The option to store the current ID_Folder in the session \ cookies is not suitable, because two different pages with different folders can be opened, respectively. It turns out that only <input type="hidden" /> . Here's the question, how do you put parameters from input hidden into Ajax.ActionLink ?

Most likely, because The page is generated BEFORE, as Ajax, we select the folder :) Then how? Maybe through OnBegin somehow? Or the only way to "handle" through jQuery to carry out a query and there already get a value with hidden value? The only way?


I found a solution, in my opinion, a very crutch (as for a guy with 11k reputation): https://stackoverflow.com/a/15104152/4727475

Looking further.

UPD.

This solution does not work for ActionLink - OnBegin works, but the URL is saved and changed after the Ajax request is sent. Alas, a little blood can not do (We'll have to draw a standard ajax through jQuery

    1 answer 1

    Indeed, the simple way to pass parameters to ActionLink does not work out, all decisions will be similar to the one you brought up in the question.

    It seems to me that it will be easier to use the usual form:

     <!-- здесь блоки с папками и файлами --> @using (Ajax.BeginForm("MoveToFolder", new AjaxOptions())) { <input type="hidden" name="ID_Folder" id="ID_Folder" /> <input type="hidden" name="ID_File" id="ID_File" /> <input type="submit" value="Переместить в папку" /> } 

    (The form will be sent by the Post method, put the [HttpPost] attribute over the action, or specify the Get method in AjaxOptions )

    If you just need a link to submit the form, then use something like:

     <a href="javascript:void(0);" onclick="$(this).closest('form').submit(); return false;">Переместить в папку</a> 

    I assume that you have handlers for the user to select a folder and file. In it we set the values ​​of the corresponding hidden fields, like this:

     $('.dirs').on('click', function () { var currentFolderId = $(this).data('id'); $('#ID_Folder').val(currentFolderId); }); $('.files').on('click', function () { var currentFileId = $(this).data('id'); $('#ID_File').val(currentFileId); }); 

    Also do not forget to include the Microsoft.jQuery.Unobtrusive.Ajax nuget and the jquery.unobtrusive-ajax.js .