简体   繁体   English

将jQuery与ASP.NET MVC结合使用

[英]Using jQuery with ASP.NET MVC

I can`t use jquery much so i need help please. 我不能使用jQuery,所以请帮忙。 I am using jQuery UI dialog like here. 我在这里使用jQuery UI对话框。 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. 每行都是具有其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. 当我单击链接transferTo(单击每行上的Edit链接后显示)时,我需要将用户ID传递给jquery函数,并在对话框的Click事件TransferTo中使用它。 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)) 当用户单击对话框中的TransferTo按钮时,它必须从对话框的下拉列表中获取id参数和所选值,并在与数据库通信的C#函数中使用它(repository.Edit(id,selectedValue))

i would have done it this way: 我会以这种方式做到的:

implement your link to call a javascript function that will open the dialog. 实现您的链接,以调用将打开对话框的javascript函数。

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

the javascript should look like this: javascript应该看起来像这样:

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 PS:如果您提到autoOpen,则结尾的open方法不是强制性的:true

Your Edit link button should have the Id in it's name. 您的“编辑”链接按钮的名称中应包含ID。 Something like "lnkEdit_{id}" where {id} is the actual Id of the item. 类似于“ lnkEdit_ {id}”,其中{id}是项目的实际ID。

In the $("#transferToLink").click event, parse out the Id from $(this) and set it in a HiddenValue field on the form. 在$(“#transferToLink”)。click事件中,从$(this)解析ID并将其设置在表单上的HiddenValue字段中。

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 唯一的缺点是您需要一个额外的“隐藏”字段来存储温度值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM