【问题标题】:modal popup from Controller .NET MVC来自 Controller .NET MVC 的模式弹出窗口
【发布时间】:2018-03-11 05:18:17
【问题描述】:

在我的索引视图中。我有一个带有操作链接的表格。在操作链接中,我在参数的基础上传递了一些参数,如果查询结果为空,我将执行查询我想显示索引视图中存在的模式。 我的桌子是。

@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>
}

接收传递参数的控制器函数是。

    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>

【问题讨论】:

  • 添加一个额外的参数,并在js中添加条件,如果特定参数值为null则显示弹出窗口。
  • 你可以像this一样使用ajax。

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


【解决方案1】:

您可以在控制器中使用 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');
        }
    }
});

控制器

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

【讨论】:

  • 非常感谢 :)
猜你喜欢
  • 2012-07-17
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
相关资源
最近更新 更多