iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 3
0
Modern Web

零經驗ASP .NET Core 30 DAY全紀錄系列 第 3

零經驗 .NET Core 30 DAY----- Day3 Layout及頁面跳轉

  • 分享至 

  • twitterImage
  •  

今天要從Layout開始瞭解,因為我要把它給!換!掉!目標進度是做完頁面跳轉/images/emoticon/emoticon08.gif
那麼下面是今天的筆記和紀錄。

Layout的基本觀念

一、主版面

首先,之前看到的頁面是預設的主模板。
https://ithelp.ithome.com.tw/upload/images/20200909/20130030C2MAksYPPz.png
設定主模板的檔案是在Views目錄底下的ViewStart.cshtml,Layout的值就是主模板頁面,所以要換成其他View做為主板只需修改這邊的設定即可(將""內的Layout換掉)。

@{
    Layout = "_Layout";
}

二、RenderBody()、ViewData、ViewBag、TempData 介紹

  • @RenderBody()
    在Views/Shared底下的Layout.cshtml中你會看到@RenderBody(),它主要的工作是將每個.cshtml的內容塞進去,這樣的話就可以把這一頁想表現的東西在其他.cshtml中另外定義了。
<div class="container">
                    <main role="main" class="pb-3">
                        @RenderBody()
                    </main>
</div>

好的,那我現在把我從網路上找到的網頁模板改一改套上去。
模板來源:https://templated.co/typify
https://ithelp.ithome.com.tw/upload/images/20200909/20130030JT3uISoiEf.png

  • @ViewData[]
    將資料傳遞給對應的 view,使用方式是
ViewData [“名稱”] = “字串”
ViewData [“名稱”] = 數值

此方法你可以在頁面中留個位子,之後再填上想填的內容。

  • @ViewBag
    將資料傳遞給對應的 view,使用方式是
ViewBag.Key = 值

此方法和上面的ViewData一樣,只存在於當下的請求, 導頁後就被清空,兩者都是將後端資料在前端顯示的方法之一,和ViewData最大的不同在於ViewBag能產生"動態屬性"。

  • TempData
    TempData基本上使用方法與ViewData一模一樣,差別在於TempData是將資料存放在 Session,當頁面導到不同的Action時,可以取得到上一個Action所存放在TempData的資料,之後它的生命就結束了,只有一次性!

三、頁面跳轉方法介紹

接著,來看看MVC架構中controller怎麼切換views的吧,以下是我在網路上看到的三種基本頁面跳轉方法。

  • Return View();
	public ActionResult NAME()
    {
        return View();//返回NAME VIEWS
        return View("Create");//返回Create VIEWS
    }
  • Return Redirect();
public ActionResult NAME (string a, int b)
    {
        return Redirect("NAME ");//參數可為空的跳轉方法
        return Redirect("NAME?b=6");//參數若必須傳遞數值,不可為空,可使用此方法
    }
  • Return RedirectToAction();
    public ActionResult NAME (string a, int b)
    {
        return RedirectToAction("ActionName ", " ControllerName ");
    }

接下來紀錄一下我今天的實作
首先來到我的首頁index.cshtml

@{
    ViewData["Title"] = "Home Page";
}

    <ul class="actions">
        <li><a class="button special" asp-area="" asp-controller="shome" asp-action="Register">Register</a></li>
        <li><a class="button special" asp-area="" asp-controller="shome" asp-action="Login">Login</a></li>
    </ul>

我建了兩個button,對應到day2中我設計的註冊&登入,然後就像前面提到的,@RenderBody()會將我們的程式塞進去,所以我的畫面就變成
https://ithelp.ithome.com.tw/upload/images/20200909/20130030JT3uISoiEf.png

接下來試試頁面跳轉,把asp-controller和asp-action填上去

asp-controller="shome" asp-action="Register"

接著來處理controller跟views

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MvcMovie.Models;

namespace MvcMovie.Controllers
{
    public class shomeController : Controller
    {
        
        
        public ActionResult Register()
        {
            return View();
        }
    }
}

上面就是我的controller內容,跳頁的方式上面介紹了三種,我選擇對我來說最簡單的方式,直接return view()。

再來,我在views目錄下建立一個對應controller的目錄(目錄名跟controller的名字一樣),然後建立register.cshtml和login.cshtml頁面。
下面是我的register.cshtml,form這裡暫時先不管他,之後再來學怎麼將form送出並存入資料庫。

@{
    ViewData["Title"] = "Register Page";
}
    <ul class="actions">
    <div> 
     <form asp-action="" method="post">
        <div class="row">
            ID:
            <div class="col"><input name="UID" class="form-control" style="color:blue;"/></div>
            PWD:
            <div class="col"><input name="PWD" class="form-control" style="color:blue;"/></div>
            <div class="col">
               <li><input value="Send" type="submit"></li>
            </div>
        </div>
    </form>
    </div>
    </ul>

今天的成果
https://ithelp.ithome.com.tw/upload/images/20200909/20130030JT3uISoiEf.png

click一下register button
https://ithelp.ithome.com.tw/upload/images/20200909/20130030YbzkQBBokM.png

DAY3心得:要學的東西真的很多,GOOGLE真的是個好老師,我都邊查遍做,觀念上還有很多不太清楚跟明白的地方,在實作的過程中,希望可以越來越清楚,最後,CSS也好難啊/images/emoticon/emoticon02.gif,版面的美化我放到後面處理,希望這幾天能速速把功能完成。


上一篇
零經驗 .NET Core 30 DAY----- Day2 從零開始的設計
下一篇
零經驗 .NET Core 30 DAY----- Day4 Entity Framework Core 對應資料庫
系列文
零經驗ASP .NET Core 30 DAY全紀錄30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言