Question: MVC controller multiple views and routing issue

Question

MVC controller multiple views and routing issue

Answers 1
Added at 2016-12-28 11:12
Tags
Question

At the moment we have view name 'Self Deposit' and a controller to back it. The view is a stand alone page that built with partial views: Example Folder name: SelfDeposit Main View file: _LayoutSelfDeposit.cshtml Partial Views: Register.cshtml, Pending.cshtml etc.

This is the main view:

<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>

This is the controller that loads the start of it:

 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);
    }

What i am trying to achieve is to have multiple Views each one with different layout and css but keep the controller the same and not copy it each time i am adding a view.

I have tried the following: Adding a folder under the main folder: SelfDeposit-->Layout1-->_LayoutSelfDeposit.cshtml But it didn't work since i couldn't figure the routing problem.

Does anyone have ideas? Thanks

Answers to

MVC controller multiple views and routing issue

nr: #1 dodano: 2016-12-28 12:12

To return a different view, you can specify the name of the view you want to return and model as follows:

return View("ViewName", yourModel);

To get the absolute path of the view, use:

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

Or you can make a partial view and can return like:

return PartialView("PartialViewName", Model);
Source Show
◀ Wstecz