複数の選択肢があります。最初は、部分ビューを使用できます。
in layout.cshtml [or in your usecase, search page]:
@Html.Partial(your-partial-view-name)
view: headerPartial.cshtml as an example:
@if(User.Identity.IsAuthenticated) {
<span>Welcome</span> @User.Identity.Name
} else {
<span>Login or Register</span>
}
これが基本ロジックです。あなたの状況では、このようなものが必要です。ただし、データベースなどからデータをロードする必要がある場合は、部分アクションを使用することをお勧めします。
// in layout or search or any page you want to render partial views:
// For example you have a controller named MyPartials:
// Put this line where you want to render header:
@Html.Action("HeaderInfo", "MyPartials")
およびアクション:
public class MyPartialsController : Controller {
public ActionResult HeaderInfo() {
var model = retrive header needed info from db
return Partial(model);
}
}
良い解決策を提案するために、問題の詳細をお知らせください
アップデート:
仮定する:
コントローラ:CustomerController
アクション:SeletedCustomer
解決:
1. モデル:
public class HeaderInfoModel { } // contains header data: name, email, phone, etc
public class SideInfoModel { } // dog1, cat2, etc
public class MainContentModel { } // holding main content data
// bringing all together
public class CustomerModel {
public HeaderInfoModel HeaderInfo { get; set; }
public SideInfoModel SideInfo { get; set; }
public MainContentModel MainContent { get; set; }
}
2.コントローラー
public class CustomerController : Controller {
public ActionResult SeletedCustomer(int id) {
// use id and retrieve data you want, from db or anywhere else you want
var model = new CustomerModel {
HeaderInfo = SomeMethodToFetchHeaderData(id),
SideInfo = SomeMethodToFetchSideData(id),
MainContent = SomeMethodToFetchMainContent(id)
};
return View(model);
}
}
3. メイン ビュー:Customer/SeletedCustomer.cshtml
@model CustomerModel
<div id="header">
<!-- put this where you want to render header -->
@Html.DisplayFor(model => model.HeaderInfo)
</div>
<div id="side">
<!-- put this where you want to render side -->
@Html.DisplayFor(model => model.SideInfo)
</div>
<div id="main">
<!-- put this where you want to render main content -->
@Html.DisplayFor(model => model.MainContent)
</div>
4.あなたの状況では、表示テンプレートを使用するのが最も簡単な方法です。または. DisplayTemplates
_ ~/Views/Customer/
_ ~/Views/Shared
提供されたモデルをどこでどのように使用するかによって異なります。繰り返しますが、あなたの場合、これを作成することをお勧めします: ~/Views/Customer/DisplayTemplates/
. 次に、そのフォルダーにいくつかのビューを追加します。以下で説明します。
4.1. HeaderInfoModel.cshtml
で作成~/Views/Customer/DisplayTemplates/
:
@model HeaderInfoModel
<div>
implement your model presentation, I mean, show the data how you want
</div>
4.2. SideInfoModel.cshtml
で作成~/Views/Customer/DisplayTemplates/
:
@model SideInfoModel
<div>
implement your model presentation, I mean, show the data how you want
</div>
4.3. MainContentModel.cshtml
で作成~/Views/Customer/DisplayTemplates/
:
@model MainContentModel
<div>
implement your model presentation, I mean, show the data how you want
</div>
それだ。終わり!あなたの状況では、私が提案できる最善の方法はそれです。ただし、複数の選択肢があります。それはあなたのプロジェクトに完全に依存しています。