1. 程式人生 > >MVC強類型視圖與model關鍵字

MVC強類型視圖與model關鍵字

dev 智能 wid body you return 情況 view視圖 div

在開發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實體類的字段了

技術分享圖片

MVC強類型視圖與model關鍵字