MVC強型別檢視與model關鍵字
阿新 • • 發佈:2018-12-31
在開發mvc專案過程中,如果需要在view檢視中進行(model欄位繫結,自動提示)模型繫結 ;在不清楚欄位,或者欄位較多的情況下,是非常有必要的!
下面咱們具體實現一下
1、首先建立Sent控制器
2、然後再Models資料夾中建立一個實體類modelTex
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication1.Models { public class modelTex {public string Only; } }
3、然後再Sent控制器中建立Tex()的方法
public ActionResult Tex() { modelTex mod = new modelTex(); mod.Only = "1"; ViewBag.moddate = mod.Only; return View(mod); }
4、新增Tex檢視
5、在Tex中引用using WebApplication1.Models;名稱空間 (WebApplication1是我的專案名稱)
然後在選擇繫結的實體類
modelTex modtext = ViewBag.moddate as modelTex;
@using WebApplication1.Models; @{ Layout = null; modelTex modtext = ViewBag.moddate as modelTex; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Tex</title> </head> <body> <div>model<br /> @ViewBag.moddate @modtext.Only//實現modelTex類中的欄位自動智慧提示(強型別檢視) </div> </body> </html>
接下來就可以在view檢視中就可以自動使用@modeltext點modeltext實體類的欄位了