如何在没有从ASP.NET MVC中的客户端进行ajax调用的情况下从控制器调用模式弹出窗口。 [英] How to call modal popup from controller without making ajax call from client side in ASP.NET MVC.

查看:53
本文介绍了如何在没有从ASP.NET MVC中的客户端进行ajax调用的情况下从控制器调用模式弹出窗口。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找代码来从代码后面调用模式弹出窗口,就像我们在asp.net中做的那样



我尝试了什么:



i正在寻找代码从代码后面调用模态弹出窗口,就像我们在asp.net中做的那样

解决方案

您需要花时间了解Web应用程序架构。即使Web表单看起来像你正在做的那样,你也无法从你的代码隐藏中调用javascript代码。就MVC而言,你没有通过控制器与客户进行任何交互,这一切都在视图中完成。



所以你有一个属性你的模型如



 public class TestModel 
{
public bool ShowDialog {get;组; }
}





在您的控制器中,您可以根据相关条件将其设置为真



 public ActionResult Test()
{
TestModel model = new TestModel();

if(someCondition)
{
model.ShowDialog = true;
}

return View(model);
}





然后在你的视图中,如果该变量为真,你将显示模态,所以这里我使用的是jQuery UI 对话框插件



 <   script     type   =  text / javascript >  
function show(){


。#dialog)对话框();
}
< / 脚本 >

@ if (Model.ShowDialog)
{
< div id = 对话框 title = 测试对话框 >
< < span class =code-leadattribute> p
> Hello world < / p >
< / div >

< script > show(); < / script >
}


i am looking for code to call a modal popup from code behind like we are doing in asp.net

What I have tried:

i am looking for code to call a modal popup from code behind like we are doing in asp.net

解决方案

You need to take time to understand the web application architecture. You can't call javascript code from your code-behind even though web forms made it look like that is what you were doing. In terms of MVC you don't have any interaction with the client via the controller, that is all done in the view.

So you'd have a property on your model like

public class TestModel
{
    public bool ShowDialog { get; set; }
}



In your controller you would set that to be true based on your relevant conditions

public ActionResult Test()
{
    TestModel model = new TestModel();

    if (someCondition)
    {
        model.ShowDialog = true;
    }

    return View(model);
}



Then in your view you would show the modal if that variable is true, so here I am using jQuery UI's "dialog" plug-in

<script type="text/javascript">
    function show() {


("#dialog").dialog(); } </script> @if (Model.ShowDialog) { <div id="dialog" title="Test dialog"> <p>Hello world</p> </div> <script>show();</script> }


这篇关于如何在没有从ASP.NET MVC中的客户端进行ajax调用的情况下从控制器调用模式弹出窗口。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆