Search code examples
asp.net-corerazor-pages

How can we make the general view, because code is reusing in different web pages?


How can we make the general view of following web pages. From that general view we can implement if conditions to redirect to the view. here is the files picture: Calling all weeks from links in index.cshtml my cshtml files looks like:

week3.cshtml

@{
    ViewData["Title"] = "Testing Weeks API";
    Layout = "~/Views/Shared/_GameLayout.cshtml";

}
<script src="/lib/pixi.js/browser/pixi.js" ></script>
<script src="/OtherJS/pixi-viewport.js"></script>
<script src="/OtherJS/pixi-scrollbox.js"></script>
<script src="~/js/week-3.js" type="module"></script>

week4.cshtml

@{
    ViewData["Title"] = "Testing Weeks API";
    Layout = "~/Views/Shared/_GameLayout.cshtml";

}
<script src="/lib/pixi.js/browser/pixi.js" ></script>
<script src="/OtherJS/pixi-viewport.js"></script>
<script src="/OtherJS/pixi-scrollbox.js"></script>
<script src="~/js/week-4.js" type="module"></script>

I just dont want 10 weeks views file, I need just one weeks.cshtml from where I can redirect to links using if condition. Thanks in Advance for solution


Solution

  • just pass a value in viewbag and put a condition on it in your controller file. If that condition fulfills than show week3 else show week4.

     public IActionResult Week()
        {
            ViewBag.Name = 1;
            return View();
        }
    

    then use it in your Weekcshtml file where you are displaying all weeks.

    @if(ViewBag.Name == 1){ 
        <script src="~/js/week-3.js" type="module"></script>
        }
    else{
        <script src="~/js/week-4.js" type="module"></script>
        }