--- tags: ASP.NET MVC 5.X --- # 1005 - MVC定義,建立Model 的資料定義,把 Controller 上使用 Model 傳給 View,在 View 用 Model 的值 ## 已針對不同的Action,有不同的Model ### 會員資料搜尋列表:http://127.0.0.1:8894/Practice/ListPractice #### MVC 的對應 > **Controller**:PracticeController.ListPractice.cs > **Model**:MemberSearchList.cs > **View**:ListPractice.cshtml > **JavaScript**:MemberSearchList.js - SelectButton ### 新增會員資料:http://127.0.0.1:8894/Practice/AddPractice #### MVC 的對應 > **Controller**:PracticeController.AddPractice.cs > **Model**:Detail.cs > **View**:AddPractice.cshtml > **JavaScript**:MemberSearchList.js - AddButton ----- ### 定義 Model:Detail.cs ![](https://hackmd.io/_uploads/HJweLiigp.png) ### Controller:PracticeController.AddPractice.cs 上使用 Model傳給 View ![](https://hackmd.io/_uploads/SyHDFsiep.png) ### 畫面的調整 View:AddPractice.cshtml ![](https://hackmd.io/_uploads/S1oYhijg6.png) ![](https://hackmd.io/_uploads/H1SE6soxT.png) ### AddPractice.cshtml 此頁的排版參考 http://127.0.0.1:8894/Etkt00/Detail ``` @using (Html.BeginFormSubmit("DetailForm")) { <table class="tblvertical" width="100%"> <tr> <!--會員姓名--> <th nowrap="nowrap" style="width: 10%"> <i>*</i>會員姓名 </th> <td width="40%"> @Html.InputTextBoxFor(m => m.Name, Size: 20) </td> <!--生日--> <th nowrap="nowrap" style="width: 10%"> <i>*</i>生日 </th> <td width="40%"> @Html.InputTextBoxFor(m => m.Birth) </td> </tr> <tr> <!--身分證字號--> <th nowrap="nowrap"> <i>*</i>身分證字號 </th> <td> @Html.InputTextBoxFor(m => m.Identify, TitleName: @Model.Identify, Size: 10) </td> <!-- 手機號碼--> <th nowrap="nowrap"> 手機號碼 </th> <td> @Html.InputTextBoxFor(m => m.Phone, Size: 10) </td> </tr> <tr> <!--性別--> <th nowrap="nowrap"> 性別 </th> <td> @Html.InputRadioButtonListFor(m => m.Gender, Model.Sexdict) </td> <!--國家∕城市--> <th nowrap="nowrap"> 國家∕城市 </th> <td> @Html.InputTextBoxFor(m => m.Country, Size: 5) @Html.InputTextBoxFor(m => m.City, Size: 5) </td> </tr> <tr> <!--地址--> <th nowrap="nowrap"> 地址 </th> <td colspan="3"> @Html.InputTextBoxFor(m => m.Address, Size: 100) </td> </tr> </table> <!--新增按鈕--> @Html.ButtonSubmit("Practice", "AddPractice", "AddButton", Resource.Button_Add, className: "btn") <!--取消按鈕--> @Html.ButtonSubmit("Practice", "ListPractice", "CancelButton", Resource.Button_Cancel, className: "btn") } ``` ![](https://hackmd.io/_uploads/SJ8EP0sxa.png) ### MemberSearchList.js > 新增按鈕:按下新增時,需檢查必填欄位資料是否正確,資料有誤請跳出錯誤訊息,資料無誤才可送出。 取消按鈕:按下取消時,返回會員資料搜尋頁(Practice/ListPractice)。 ![](https://hackmd.io/_uploads/ByBQI2igT.png) ### 引用 SQL: Entity 檔案上: Para : 使用外部方法(得到SQL參數需要用的),Model 跟 資料庫的中介商 建立 DB Type 跟 一般 Type 是因為要把 SQL抓到的東西給Model 去呈現在 View ### Model 的值寫給 EntityPara ,然後 commandText 的內容抓EntityPara