gpt4 book ai didi

c# - MVC Controller 多 View 和路由问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:36 25 4
gpt4 key购买 nike

目前我们有 View 名称“Self Deposit”和一个支持它的 Controller 。该 View 是使用部分 View 构建的独立页面:例子文件夹名称:SelfDeposit主视图文件:_LayoutSelfDeposit.cshtml部分 View :Register.cshtml、Pending.cshtml 等

这是主视图:

<html dir="@(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft ? "rtl" : "ltr")">
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/SelfDeposit_css")
@Scripts.Render("~/bundles/scripts")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval")
@if (System.Globalization.CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft)
{
@Styles.Render("~/Content/BootstapRtl_css")
}
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700|PT+Sans" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<a href="#" class="logo"></a>
</div>
</div>
</div>
@RenderBody()
<div class="container text-center">
<img src="~/Content/SelfDeposit/footer-icons.png" class="img-responsive footer-icons" />
</div>
@Html.Partial("ThirdPartyPixels/GoogleTagManager")
</body>
</html>

这是加载它开始的 Controller :

 private readonly ForexDbContext _Db = DbLoader.GetDb();
// GET: SelfDeposit
public ActionResult Index()
{
return RedirectToAction(nameof(this.Register));
}
public async Task<ActionResult> Register()
{
ViewBag.CountryId = await GetCountiesAsync();
var model = new SelfDepositRegistrationViewModel { };
if (TempData[Main.LEAD_REG_DETAILS_FOR_OPEN_ACCOUNT] != null && TempData[Main.LEAD_REG_DETAILS_FOR_OPEN_ACCOUNT] is LeadRegistered)
{
var leadRegistered = TempData[Main.LEAD_REG_DETAILS_FOR_OPEN_ACCOUNT] as LeadRegistered;
ViewBag.LeadRegisteredDetails = leadRegistered;
model.FirstName = leadRegistered.FirstName;
model.LastName = leadRegistered.LastName;
model.Email = leadRegistered.Email;
model.PhoneNumber = leadRegistered.Phone;
model.PhoneCountryCode = leadRegistered.PhoneCountry;
}

return View(model);
}

我想要实现的是拥有多个 View ,每个 View 都具有不同的布局和 css,但保持 Controller 相同,而不是在每次添加 View 时都复制它。

我尝试了以下方法:在主文件夹下添加一个文件夹:SelfDeposit-->Layout1-->_LayoutSelfDeposit.cshtml但它没有用,因为我无法解决路由问题。

有人有想法吗?谢谢

最佳答案

要返回不同的 View ,您可以指定要返回的 View 的名称和模型,如下所示:

return View("ViewName", yourModel);

要获取 View 的绝对路径,请使用:

return View("~/Views/FolderName/ViewName.cshtml");

或者你可以创建局部 View 并返回:

return PartialView("PartialViewName", Model);

关于c# - MVC Controller 多 View 和路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41360477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com