Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 8-Feb-17 23:11pm
Comments
Karthik_Mahalingam 9-Feb-17 5:13am    
Not possible.

1 solution

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

Razor
<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>
}
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 9-Feb-17 5:14am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900