在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 關注
- 1253 瀏覽
添加回答
舉報
0/150
提交
取消