在我的索引视图中。我有一个带有操作链接的表。在Action链接中,如果查询结果为null,我将在我执行查询的参数的基础上传递一些参数我想在索引视图中显示模态。 我的表是。
@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>
答案 0 :(得分: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">×</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");
}
}