Question: MVC controller multiple views and routing issue


MVC controller multiple views and routing issue

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

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")">
    <meta name="viewport" content="width=device-width" />
    @if (System.Globalization.CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft)
    <link href=",700|PT+Sans" rel="stylesheet">
    <div class="container">
        <div class="row">
            <div class="col-md-12 text-center">
                <a href="#" class="logo"></a>
    <div class="container text-center">
        <img src="~/Content/SelfDeposit/footer-icons.png" class="img-responsive footer-icons" />

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