javascript - 来自 Controller .NET MVC 的模态弹出窗口

标签 javascript c# jquery asp.net-mvc jquery-ui

在我的索引 View 中。我有一个带有操作链接的表。在 Action 链接中,我在参数的基础上传递了一些参数,如果查询结果为空,我执行查询我想显示索引 View 中存在的模态。 我的 table 是。

@foreach(var j in Model)
{
   <tr>
       <td>@Html.DisplayFor(modelItem => j.job_title)</td>
       <td>@Html.DisplayFor(modelItem => j.job_description)</td>
       <td>@Html.DisplayFor(modelItem => j.apply_before)</td>
       <td>@Html.ActionLink( "Apply","applyingjobs","Student",                        
            new {                                                   
                id= @TempData["data"]
                },
             null
                 )

       </td>  

    </tr>
}

接收传递参数的我的 Controller 函数是。

    public ActionResult applyingjobs(String id)
    {
        SqlConnection con = new SqlConnection("xxxxxxxxxxx");
        SqlCommand cmd = new SqlCommand();
        con.Open();
        cmd.CommandText = "select count(*)from Users where id='" + id + "'and " + "type = " + 2 + " and exe!= null and qua!= null" ;
        cmd.Connection = con;
        Int32 countnamefieldadd = (Int32)cmd.ExecuteScalar();
        if (countnamefieldadd == 0)
        {
           //here I want to show modal which is present in Index Page
        }
        else
        {
            return RedirectToAction("Index", "Student", new
            {
                id = id,
            });

        }

        return RedirectToAction("Index", "Student", new
        {
            id = id,
        });          
    }

我的模态代码是

           <div id="modal_dialog" style="display: none">


              //  Modal content

                 </div>

调用Modal的脚本是

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

         $("#modal_dialog").dialog({
             title: "Add Record",

             open: function (type, data) { $(this).parent().appendTo("form"); },
             modal: true
         });
         return false;
 })
</script>

最佳答案

您可以在您的 Controller 中使用 Tempdata 来保留该值并将其用作检查查询是否返回记录的标志。

试试这个。希望对您有所帮助:)

HTML

@Html.ActionLink("Apply", "applyingjobs", "Employee")

<div>
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Modal Header</h4>
                </div>
                <div class="modal-body">
                    <p>Some text in the modal.</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>

        </div>
    </div>
</div>

脚本

  $(document).ready(function ()
{
    if ('@TempData["value"]' != "" || '@TempData["value"]' != null)
    {
        if ('@TempData["value"]' == "No Records")
        {
            $("#myModal").modal('show');
        }
        else {
            $("#myModal").modal('hide');
        }
    }
});

Controller

public ActionResult applyingjobs()
    {
        var c = Repository.SelectAll().ToList();
        if (c.Count() > 0)
        {
            return RedirectToAction("Create");
        }
        else
        {
            TempData["value"] = "No Records";
            return RedirectToAction("Create");
        }
    }

关于javascript - 来自 Controller .NET MVC 的模态弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46486488/

相关文章:

javascript - 在 JavaScript 中禁用 Opera 功能键

javascript - 如果该事件附加到不存在的元素,是否可以触发该事件

c# - 使用 JSON.Net 的 Web API 的驼峰式大小写问题

javascript - 如何使用 Response.Write ("&lt;script&gt;alert(' Center Page')&lt;/script&gt;") 设置警报页面样式

javascript - 提交php文件而不刷新

javascript - y 滚动条上固定菜单的眨眼/闪烁问题

javascript - 样式化 JQuery UI 自动完成

javascript - 不和谐收集器 react 过滤器方式设置最大限制

c# - Silverlight 4 C# - 如何在自定义用户控件的文本位置显示弹出窗口?

javascript - 如何根据下拉选择使文本框只读?