我有一个名为Search.cshtml的局部视图,我在/ home /索引显示。在这个文件中,我有一个HTML表单和搜索与搜索结果拉回/浏览/会计/索引。我想显示的模式弹出的div是在搜索视图的结果。
当我点击搜索代码(输入提交)下面我得到一个空的模式。
我还是个新手,当涉及到MVC。我想我对堆栈溢出发现了几个不同的结果,但我一直没能找到解决方案。下面的代码至少给我一个模式弹出,虽然它是空白。
我失去了疯狂的东西简单?我曾尝试在下面的模态体(Html.Action,的RenderAction,局部,的RenderPartial)和似乎没有任何工作的一切。另外,我是找错了树那里?
我有一对夫妇的截图和代码如下。
/Home/Index with Search partial view
Search.cshtml
@model CustomerRelationshipManager.Models.Search
@{ViewBag.Title = "Search";}
@using (Html.BeginForm("Index", "Accounts", new { id = "searchForm" }))
{
<div style="border: solid 1px #ccc; padding: 30px 0 30px 30px; border-radius: 5px;
width: 325px; margin: auto; display: table;">
<table>
<tr>
<td valign="top">
Search By:
</td>
<td>
@Html.DropDownList("Search_Type", "Search_Type")
</td>
</tr>
<tr>
<td valign="top"></td>
<td>
@Html.TextBox("Search_String")
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" data-toggle="modal" data-target="#myModal" value="Search" />
</td>
</tr>
</table>
</div>
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
@{Html.Action("Index","Accounts");}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
}
HomeController.cs
public ActionResult Search()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "Account Number", Value = "Account_ID" });
items.Add(new SelectListItem() { Text = "Last Name", Value = "Last_Name" });
items.Add(new SelectListItem() { Text = "Phone Number", Value = "Phone_Number" });
ViewBag.Search_Type = items;
return PartialView();
}
[HttpPost]
[AllowAnonymous]
public ActionResult Search(Search search)
{
return PartialView("~/Accounts/Index");
//return RedirectToAction("Index", "Accounts");
}
AccountController.cs(我想打电话从这个控制器指数())
public ActionResult Index(string Search_Type, string Search_String)
{
if (Search_String == null)
{
var accounts = db.Accounts
.Include(a => a.Account_Type)
.Include(a => a.Account_Person)
.Include(a => a.Account_Address)
.Include(a => a.Account_Contact);
return PartialView(accounts.ToList());
}
else
{
if (Search_Type == "Account_ID")
{
var accounts = db.Accounts
.Include(a => a.Account_Type)
.Include(a => a.Account_Person)
.Include(a => a.Account_Address)
.Include(a => a.Account_Contact)
.Where(a => a.Account_ID.ToString() == Search_String);
return PartialView(accounts.ToList());
}
else if (Search_Type == "Last_Name")
{
var accounts = db.Accounts
.Include(a => a.Account_Type)
.Include(a => a.Account_Person)
.Where(b => b.Account_Person.Any(c => c.Person.Last_Name.StartsWith(Search_String)))
.Include(a => a.Account_Contact)
.Include(a => a.Account_Address);
return PartialView(accounts.ToList());
}
else if (Search_Type == "Phone_Number")
{
var accounts = db.Accounts
.Include(a => a.Account_Type)
.Include(a => a.Account_Person)
.Include(a => a.Account_Contact)
.Where(b => b.Account_Contact.Any(c => c.Contact.Value == Search_String && c.Contact.Contact_Type.Name.Contains("Phone")))
.Include(a => a.Account_Address);
return PartialView(accounts.ToList());
}
else
{
var accounts = db.Accounts
.Include(a => a.Account_Type)
.Include(a => a.Account_Person)
.Include(a => a.Account_Address)
.Include(a => a.Account_Contact);
return PartialView(accounts.ToList());
}
}
账户Index.cshtml(我想在模式弹出来显示这一点)
@model IEnumerable<CustomerRelationshipManager.Models.Account>
@{
ViewBag.Title = "Home";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>Account #</th>
<th>Contact Name(s)</th>
<th>Address</th>
<th>Contact</th>
</tr>
w2
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Account_ID)
<span> </span>
</td>
<td>
@foreach (var i in item.Account_Person)
{
<span>
<b>@Html.DisplayFor(x => i.Person_Type.Name)</b>
<br />
@Html.DisplayFor(x => i.Person.First_Name)
@Html.DisplayFor(x => i.Person.Last_Name)
</span>
<br />
}
</td>
<td>
@foreach (var i in item.Account_Address)
{
<span>
<b>@Html.DisplayFor(x => i.Address_Type.Name)</b>
<br />
@Html.DisplayFor(x => i.Address.Address1)
<br />
@Html.DisplayFor(x => i.Address.Address2)
@if (i.Address.Address2 != null)
{ <br />}
@Html.DisplayFor(x => i.Address.City)
@Html.DisplayFor(x => i.Address.State)
@Html.DisplayFor(x => i.Address.Postal_Code)
<br />
@Html.DisplayFor(x => i.Address.Country)
<br />
</span>
}
</td>
<td>
@foreach (var i in item.Account_Contact)
{
<span>
<b>@Html.DisplayFor(x => i.Contact.Contact_Type.Name)</b>
<br />
@Html.DisplayFor(x => i.Contact.Value)
<br />
</span>
}
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Account_ID }) |
@Html.ActionLink("Details", "Details", new { id = item.Account_ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Account_ID })
</td>
</tr>
}
根据您当前的代码,当用户点击提交按钮,它会做一个正常的表单提交作为你的提交按钮是一个form
标签内。为了您的使用情况下,你应该使用JavaScript,使一个AJAX调用您的操作方法,其中将使用submit
和search_type
参数,以获得经过滤的数据,并返回一个局部视图结果被劫持正常的形式search_string
事件。这部分观点的结果是你想要的模态对话框内显示的HTML标记。一旦你的Ajax调用接收来自服务器的响应,更新模式对话框的主体内容与此响应并触发模式对话框。
@using (Html.BeginForm("Index", "Accounts", FormMethod.Post, new { id = "searchForm" }))
{
<div>
<input type="text" name="Search_String" />
<input type="submit" id="submit" value="Search" />
</div>
}
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
现在有一些JavaScript代码,听取事件提交搜索表单上并停止正常行为(正常表单提交),而是做一个ajax形式提交。
$(document).ready(function () {
$('#searchForm').submit(function (e) {
e.preventDefault();
var $form = $(this);
$.post($form.attr("action"), $form.serialize()).done(function (res) {
$mymodal = $("#myModal");
//update the modal's body with the response received
$mymodal.find("div.modal-body").html(res);
// Show the modal
$mymodal.modal("show");
});
});
});
现在,你必须确保你的指数操作方法返回的局部视图(所以它不会执行任何布局代码,但只是查看代码)。
[HttpPost]
public ActionResult Index(string Search_Type, string Search_String)
{
// Your existing filtering code goes here.
return PartialView(accounts.ToList());
}
我想到了!
@model CustomerRelationshipManager.Models.Search
@{
ViewBag.Title = "Search";
}
@using (Html.BeginForm("Index", "Accounts", FormMethod.Post, new { id = "searchForm" }))
{
<div style="border: solid 1px #ccc; padding: 30px 0 30px 30px; border-radius: 5px;
width: 325px; margin: auto; display: table;">
<table>
<tr>
<td valign="top">
Search By:
</td>
<td>
@Html.DropDownList("Search_Type", "Search_Type")
</td>
</tr>
<tr>
<td valign="top"></td>
<td>
@Html.TextBox("Search_String")
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" id="submit" value="Search" />
</td>
</tr>
</table>
</div>
}
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
<script type="text/javascript">
$(document).ready(function () {
$('#searchForm').submit(function (e) {
e.preventDefault();
var $form = $(this);
$.post($form.attr("action"), $form.serialize()).done(function (res) {
$mymodal = $("#myModal");
//update the modal's body with the response received
$mymodal.find("div.modal-body").html(res);
// Show the modal
$mymodal.modal("show");
});
});
});
HomeController Search() Modal Dialog Displaying AccountsController Index()
感谢您的帮助,Shyju!