从jquery对话框中加载的局部视图返回值

时间:2021-11-21 11:20:58

I have a partialview "selectUser". On this partial view a user can search for other users. The user id will be stored in a hidden field or as a var on my view. I need to use this partial view in many places. Lets say I need to return the id of the selected user on the close event of my dialog. My question is, how do I make this partial view, loaded as a modal dialog with jquery ui, to retrun the selected value to its parent view? Is there a way to access the value directly from the parent view?

我有一个partialview“selectUser”。在该部分视图上,用户可以搜索其他用户。用户ID将存储在隐藏字段中或存储在我的视图中。我需要在很多地方使用这个局部视图。假设我需要在对话框的close事件中返回所选用户的id。我的问题是,如何将这个局部视图加载为jquery ui的模态对话框,将所选值重新转换为其父视图?有没有办法直接从父视图访问该值?

1 个解决方案

#1


2  

I think I follow what you are needing now. So on your button click you do an ajax call back to the server and include the destination field name in the call

我想我现在就听你需要的。因此,在按钮上单击,您将ajax调用回服务器,并在调用中包含目标字段名称

$.ajax({
     url: "@(Url.Action("Action", "Controller"))",
     type: "POST",
     cache: false,
     async: true,
     data: { destination: 'fieldName' },
     success: function (result) {
         $(".Content").html(result);
         AttachScript();
         Dialog.load();
     }
});

On your controller send that field to the partial view through your view model or viewbag and on the partial view put that field name in a hidden field. then in your button click you should be able to do something like this (untested)

在控制器上,通过视图模型或视图包将该字段发送到局部视图,在局部视图上将该字段名称放在隐藏字段中。然后在你的按钮点击你应该能够做这样的事情(未经测试)

function AttachScript(){
    $('.btnSubmit').on('click', function(){
        var data = $('.sharedField').val();
        $($('.HiddenField').val()).val(data);
    });
}

which will set the value of whatever field is named in your hidden field to the data. Hopefully this helps.

这将设置隐藏字段中为数据命名的任何字段的值。希望这会有所帮助。

#1


2  

I think I follow what you are needing now. So on your button click you do an ajax call back to the server and include the destination field name in the call

我想我现在就听你需要的。因此,在按钮上单击,您将ajax调用回服务器,并在调用中包含目标字段名称

$.ajax({
     url: "@(Url.Action("Action", "Controller"))",
     type: "POST",
     cache: false,
     async: true,
     data: { destination: 'fieldName' },
     success: function (result) {
         $(".Content").html(result);
         AttachScript();
         Dialog.load();
     }
});

On your controller send that field to the partial view through your view model or viewbag and on the partial view put that field name in a hidden field. then in your button click you should be able to do something like this (untested)

在控制器上,通过视图模型或视图包将该字段发送到局部视图,在局部视图上将该字段名称放在隐藏字段中。然后在你的按钮点击你应该能够做这样的事情(未经测试)

function AttachScript(){
    $('.btnSubmit').on('click', function(){
        var data = $('.sharedField').val();
        $($('.HiddenField').val()).val(data);
    });
}

which will set the value of whatever field is named in your hidden field to the data. Hopefully this helps.

这将设置隐藏字段中为数据命名的任何字段的值。希望这会有所帮助。