發表文章

目前顯示的是 4月, 2022的文章

[ASP.NET MVC] @Html.Label() 、@Html.LabelFor() 和 <label>

圖片
<input> 常常搭配 <label> 方便點選,產生的方法是 @Html.Label(),一個引數的寫法如下: @Html.Label("帳號:") // 會產生: <label for="帳號:">帳號:<label> 但是上面的結果通常不是我們要的,for 屬性要填別的 <input> 的 id ,常常是變數,這就要用兩個引數: @Html.Label("txtAccount" // 成為 for,填的是點選後會 focus 到的目標 id , "帳號:") // 成為 textContent ,顯示的文字內容 // 會產生: <label for="txtAccount">帳號:<label> 也可用第三個引數帶入 HTML 屬性,使用匿名類別 new { } : @Html.Label("txtAccount": , "帳號:" , new { @class = "label-title" } ) // 會產生: <label class="label-title" for="txtAccount">帳號:<label> 以上因為 class 是保留字所以前面要加上 @ 。另外有繫結 Model 時可用強型別的方法,例如這是從 Controller 帶往 View 的 ViewModel: public class VMLogin { public string Account { get; set; } public string Password { get; set; } } VMLogin vModel = new VMLogin(); return View(vModel); 而 View 中可用 LabelFor(...

[ASP.NET MVC] @Html.TextArea() 、@Html.TextAreaFor() 和 <textarea>

圖片
產生 <textarea> 標籤用的是 @Html.TextArea() 方法,TextArea() 很像 TextBox()。第一個引數是字串,做為 id 和 name : @Html.TextArea("comment") // 會產生: <textarea cols="20" id="comment" name="comment" rows="2"></textarea> 會預設有一個適當大小,row="2" col="20,下面會示範還可以調整。若使用第二個引數可用字串填入預設文字,也就是前後標籤包起來的內容: @Html.TextArea("comment" , "很好吃" ) // 會產生: <textarea cols="20" id="comment" name="comment" rows="2">很好吃</textarea> 或是用第二個引數帶入 HTML 屬性,使用匿名類別 new { } : @Html.TextArea("comment" , new { placeholder = "請留下您的評價" } ) // 會產生: <textarea cols="20" id="comment" name="comment" placeholder="請留下您的評價" rows="2"></textarea> 使用三個引數可一次帶入已填入文字和 HTML 屬性,例如: @Html.TextArea("comment" , "氣氛不錯" ...

[ASP.NET MVC] @Html.TextBox() 、@Html.TextBoxFor() 和 <input type="text">

圖片
<input>應該是最常用的獲得使用者資訊用的標籤了,@Html.TextBox() 使用如下,一個引數時為 id 和 name: @Html.TextBox("account") // 會產生: <input type="text" id="account" name="account" /> 若要已填入特定字串,也就是 value 值,則放在第二個引數: @Html.TextBox("account" , "John123") // 會產生: <input type="text" id="account" name="account" value="John123" /> 也可產生 HTML 屬性,要帶入 HTML Attribute 要用匿名類別 new { } 放在第三個引數: @Html.TextBox("account" , "John123" , new { required = true } ) // 會產生: <input required="True" type="text" id="account" name="account" value="John123" /> 以上會產生 reuiqred 屬性,在 form submit 時若為空,可在前端先行檢查,也可帶入 style 或不一樣的 id 或事件。另外有強型別的 TextBoxFor() 可以更快速給 Model 建立標籤,例如 Student 類別: class Student { public string Name { get; set; } public int Age { get; set; } ...

[ASP.NET MVC] @Html.ActionLink() 和 <a>

圖片
要產生 <a> 超連結標籤,用的是 @Html.ActionLink() 方法,若要產生同一個 Controller 內的 Action 超連結,例如同樣 Home Controller 中的兩個 Action: Index 和 Contact,要在 Index 中產生一個連往 Contact 的超連結,就在 Index 的 View 中使用: @Html.ActionLink("連絡我們" , "Contact") // 會產生: <a href="/Home/Contact/">連絡我們</a> 第一個引數是超連結顥示的文字,第二個引數是同 Controller 裡的其他 Action。也可超連結至其他 Controller 的 Action,例如另一個 Product Controller 的 List Action,就寫在第二和第三個引數: @Html.ActionLink("產品列表" , "List" , "Product") // 會產生: <a href="/Product/List/">產品列表</a> 另外也可以產生 HTML Attribute,例如要給這個 <a> 標籤一個 id = "linkA" 讓 JavaScript 使用,或是增加開新分頁的 target 屬性,要使用匿名類別 new { },放在第四個引數: @Html.ActionLink("產品列表" , "List" , "Product" , new { id = "linkA" , target = "_blank" } ) // 會產生: ...

[ASP.NET MVC] HTML Helpers 筆記目錄

因為先學了 HTML,習慣也熟練各個 tag,所以在 ASP.NET MVC 的 View,就很排斥使用 HTML Helper,除非為了那些驗證或範本的產生,不然我在 View 都還是手刻 HTML,說慢倒也不會,看著一個個 tag 和 Attribute 反而很踏實。直到進入職場,被前輩說一定要熟練HTML Helper,而且前輩的心血結晶也都是用 helper 寫的,才開始要來習慣 @Html 的一切,這裡也做個筆記。 @Html.ActionLink() 和 <a> @Html.TextBox() 、@Html.TextBoxFor() 和 <input type="text"> @Html.TextArea() 、@Html.TextAreaFor() 和 <textarea> @Html.Label() 、@Html.LabelFor() 和 <label> @Html.Password() 、@Html.PasswordFor() 和 <input type="password"> @Html.RadioButton() 、@Html.RadioButtonFor() 和 <input type="radio"> @Html.CheckBox() 、@HtmlCheckBoxFor() 和 <input type="checkbox"> @Html.DropDownList() 、@Html.DropDownListFor() 和 <select> 與<option> @Html.BeginForm() 和 <form> @Html.Hidden() 、@Html.HiddenFor() 和 <input type="hidden"> @Html.DisplayFor() 、@Html.DisplayTextFor()、@Html.DisplayNameFor() 和 @Html.EditorFor()

[C#] Fibonacci sequence 費氏數列

圖片
最近把 Codewars : The Millionth Fibonacci Kata 解出來,想說都出到 BigInteger 還考慮了負整數 n,大概也夠完整了吧,就想把這個主題整理一下。 費氏數列定義 遞迴函數 Recursive Function 費氏數的通解 Binet’s Formula 排列組合等價問題的解法 矩陣與log(N)求費氏數 費氏數列定義 費波那契數列 (Fibonacci sequence) 或常簡稱費氏數列,是中學數學常見、程式設計界也常見的主題,他有的一些大自然現象(像兔子、蜜蜂數量與黃金比例)我就不討論了,純粹討論數學與程式。費氏數列是如下的數列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 規律是 「每一項是前兩項的和」 ,例如 8 = 3 + 5,或 144 = 55 + 89,而 「最初兩項是 0 和 1」 (數學版本因為足標正整數從 1 開始,故最初兩項為 1 和 1,我這裡討論程式版本 index 從 0 開始,故最初兩項為 0 和 1),其遞迴關係式為: 費氏數列中的數稱為費氏數 (Fibonacci number),若有一函數 F(n) 表費氏數列中 index = n 的數,例如 n = 6 的 a 6 即 F(6) = 8,則此 F(n) 函數定義為: 這個 F(n) 的取法就成為常見問題。 遞迴函數 Recursive Function 顯然上面已經是個遞迴函數的 base case 和 recursive case 了,所以程式碼: public int Fib(int n) { if (n == 0) // base case { return 0; } if(n == 1) // base case { return 1; } return Fib(n - 2) + Fib(n - 1); // recursive case } 這是個很好訓練 Recursion 的練習,但這個作法效能...