(タイトルの謝罪..特定するのが難しかった)
コントローラーが、抽象基本クラスから派生したビューモデルのセットを含むリストを作成する状況があります。次に、表示テンプレートを使用してコンテンツを適切にレンダリングしますが、URL に移動すると、全体が 2 回レンダリングされているように見えます。これが私のコードです
public abstract class AbstractItemViewModel
{
}
public class TypeAViewModel : AbstractItemViewModel
{
public int ID { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
public class TypeBViewModel : AbstractItemViewModel
{
public string Title { get; set; }
public List<string> Items { get; set; }
}
次に、コントローラーがテスト ケースをビルドします。
public class HomeController : BaseController
{
// GET: Home
public ActionResult Index()
{
List<AbstractItemViewModel> items = new List<AbstractItemViewModel>();
items.Add(new TypeAViewModel() { ID = 1, Title = "Test A", Body = "This is some body content." });
items.Add(new TypeBViewModel() { Title = "Test B", Items = new List<string>() { "Line 1", "Line 2", "Line 3" } });
return View(items);
}
}
...そして完全を期すために、ここにベースコントローラークラスがあります(特別なものはありません)..
public class BaseController : Controller
{
}
..そしてここにビューがあります...
@using xxx.ViewModels
@model List<AbstractItemViewModel>
@{
ViewBag.Title = "Home";
}
<h1>@Model.Count()</h1>
<div class="container">
<div class="row">
@foreach (AbstractItemViewModel item in Model)
{
<div class="col-xs-12">
@Html.DisplayForModel(item)
</div>
<p>space</p>
}
</div>
</div>
.. および 2 つの表示テンプレート TypeAViewModel.cshtml
@using xxx.ViewModels
@model TypeAViewModel
<h2>@Model.Title (@Model.ID)</h2>
<p>@Model.Body</p>
.. そして ... TypeBViewModel.cshtml
@using xxx.ViewModels
@model TypeBViewModel
<h2>@Model.Title</h2>
<ul>
@foreach (string s in Model.Items)
{
<li>@s</li>
}
</ul>
出力として取得します。
2
テスト A (1) これは本文の一部です。
テスト B ライン 1 ライン 2 ライン 3 スペース
テスト A (1) これは本文の一部です。
テスト B ライン 1 ライン 2 ライン 3 スペース
ご覧のとおり、全体を 2 回レンダリングしているように見えます。ブレークポイントを配置し、Index View をステップスルーしても、ループ自体は繰り返されません。私が見逃しているものを見た人はいますか?