0

I can`t use jquery much so i need help please. I am using jQuery UI dialog like here. I have Link that opens the dialog where is rendered my partial view.

$(function () {
    $("#transferTo").dialog({
        autoOpen: false,
        height: 100,
        width: 300,
        modal: true,
        resizable: false,
        open: function (event, ui) {
            $(this).load('<%= Url.Action("TransferTo", "Pacient") %>');
        },
        buttons:
        {
            "Transfer": function () {
                // do something in database
                $(this).dialog("close");
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });
    $("#transferToLink").click(function () {
        $("#transferTo").dialog("open");
        return false;
    });
});

<a href="javascript:void()" id="transferToLink">Transfer to</a>

Partial view looks like:

<div id="transferTo">
    Zmena kliniky
<%= Html.DropDownList("klinika", ViewData["kliniky"] as SelectList)%>
</div>

In the main view there are rows with some data. Each row is user with his id. So what i want to do...

When i click on the link transferTo (it shows after click on Edit link on each row) i need to pass the id of the user to the jquery function and use it on the Click event TransferTo of the dialog. When user clicks on the TransferTo button in the dialog it must take id parameter and selected value from the dropdownlist from dialog and use it in my C# function comunicating with the database (repository.Edit(id,selectedValue))

2 Answers 2

1

i would have done it this way:

implement your link to call a javascript function that will open the dialog.

<a href="#" onclick="openPopup('@id')" id="transferToLink">Transfer to</a>

the javascript should look like this:

function openPopup(id) {
    $("#transferTo").dialog({
        autoOpen: false,
        height: 100,
        width: 300,
        modal: true,
        resizable: false,
        open: function (event, ui) {
            $.ajax({
                "url": yourActionUrl,
                "data": { myParameter: id },
                "type": "POST"
            });
        },
        buttons:
    {
        "Transfer": function () {
            // do something in database
            $(this).dialog("close");
        },
        "Cancel": function () {
            $(this).dialog("close");
        }
    }
    }).open();
}

Hope this helps.

PS: The open method at the end is not mandatory if you mention autoOpen : true

0

Your Edit link button should have the Id in it's name. Something like "lnkEdit_{id}" where {id} is the actual Id of the item.

In the $("#transferToLink").click event, parse out the Id from $(this) and set it in a HiddenValue field on the form.

Read this value in the Transfer button event in the dialog.

It's not elegant but your options are limited with this type of setup, and I have used this technique before without any problems. The only negative is that you need an extra Hidden field to store a temp value

9
  • Thank you John. I did it using ViewData and now I have problem that I do not know how to call my function from Transfer button in the dialog. "Transfer": function () { //call my function repository(ViewData["id"],selectedValue) $(this).dialog("close"); } So please, how can I call my C# function (repository(ViewData["id"],selectedValue) and how can I pass the parameter selectedValue ($("dropdownlist option:selected).text()) to that function? Thank you Commented Oct 19, 2012 at 13:31
  • You can use jQuery to make a Post to your controller action. The controller action validates the input and then calls your repository. Something like this: $.ajax({ type: "POST", url: link.href, success: function (data) { $(link).parents("tr").remove(); }, error: function (data) { alert("This user cannot be deleted"); } });
    – John Mc
    Commented Oct 19, 2012 at 14:18
  • Thank you. Now i have this: "Transfer": function () { $.post('<%= Url.Action("TransferToOk", "User") %>', { "id": '<%= ViewData["id"]%>', "role": $("#role option:selected").text() }, null, "script"); $(this).dialog("close"); } But it is returning me the first option in dropdownlist and no the selected. Do you know why pls ? Commented Oct 19, 2012 at 14:53
  • Is role the Id of your dropdown list? you should use the jquery .val() method to get the selected value from dropdowns. What is in ViewData["Id"]?
    – John Mc
    Commented Oct 19, 2012 at 15:32
  • I am sorry. I thin selected text, this is returning selected text, but it returns the first oprion (that looks like it ignore my selection. It looks like it buffer the selected text on the page load and when i select another option it ignores it:/).. in ViewData["id"] is id of the user and it's ok, there is not a problem. Commented Oct 19, 2012 at 15:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.