在ASP.NET MVC中单击按钮时呈现部分视图我在Visual Studio中创建了一个新的ASP.NET MVC 5应用程序。然后,我定义了两个模型类:public class SearchCriterionModel{
public string Keyword { get; set; }}public class SearchResultModel{
public int Id { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }}然后我创建了SearchController如下:public class SearchController : Controller{
public ActionResult Index()
{
return View();
}
public ActionResult DisplaySearchResults()
{
var model = new List<SearchResultModel>
{
new SearchResultModel { Id=1, FirstName="Peter", Surname="Pan" },
new SearchResultModel { Id=2, FirstName="Jane", Surname="Doe" }
};
return PartialView("SearchResults", model);
}}以及视图Index.cshtml(强烈键入SearchCriterionModel模型和模板编辑)和SearchResults.cshtml作为类型模型(模板列表)的局部视图。IEnumerable<SearchResultModel>这是索引视图:@model WebApplication1.Models.SearchCriterionModel@{
ViewBag.Title = "Index";}@using (Html.BeginForm()){
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SearchCriterionModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Keyword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Keyword, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Keyword, "", new { @class = "text-danger" })
</div>
</div>正如你所看到的,我加了一个div与id="searchResults"标准模板下方和编辑按钮。我要的是显示局部视图SearchResults.cshtml的div底部,但点击按钮之后。我已成功通过使用显示局部视图@Html.Partial("SearchResults", ViewBag.MyData),但是在第一次加载父视图时我将其呈现并且我已经ViewBag.MyData在Index()方法中设置了,这不是我想要的
- 2 回答
- 0 关注
- 1212 浏览
添加回答
举报
0/150
提交
取消