我有一个Index.cshtml视图,一个Filter.cshtml局部视图和Results.cshtml局部视图。我希望在用户点击搜索按钮之后,看看我的Results.cshtml parital视图中的值,以及来自Index.cshtml和Filter.cshtml的内容。在单击搜索按钮后,如何构建我的页面以查看Results.cshtml中的内容以及其他内容?
这就是我对Index.cshtml视图的看法
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ Html.RenderPartial("Filter"); }
@{ Html.RenderPartial("Results"); }
答案 0 :(得分:1)
如果你可以使用jQuery那么你可以做这样的事情
在您的视图中,您可以拥有结果容器
<强> HTML 强>
<div id="result-container"></div>
使用jQuery,您可以在点击提交时获得内容
<强>的JavaScript 强>
$(document).ready(function () {
$('#submit').click(function () {
$.ajax({
type: 'GET',
url: 'YourController/ActionThatReturnPartialView',
data: {
// your form data here
}
}).done(function (html) {
// place the partial view to the container
$('#result-container').html(html);
});
});
});
<强>控制器强>
public class YourController
{
public ActionResult ActionThatReturnPartialView()
{
// get parameters and do some logic
return PartialView(model);
}
}
答案 1 :(得分:0)
您可以使用视图模型:
public class MyViewModel
{
public string Filter { get; set; }
public string Results { get; set; }
}
然后:
public class HomeController: Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
model.Results = "this is the result";
return View(model);
}
}
并在视图中:
@model MyViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ Html.RenderPartial("Filter"); }
@if (Model.Results != null)
{
@{ Html.RenderPartial("Results");
}
并使用模板:
@model MyViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.EditorFor(x => x.Filter)
@if (Model.Results != null)
{
@Html.DisplayFor(x => x.Results)
}