ASP.NET MVC与EntityFramework入门(一)

Razor视图


视图的相关约定
1.所有的视图必须放到Views目录下
    Views\Home\index.cshtml
2.不同控制器的视图用文件夹进行分割,每个控制器都对应一个视图目录
    HomeController.cs--Views\Home\...
    MapController.cs--Views\Map\...
3.一般视图名字跟控制器的Action相对应(非必须)

4.多个控制器公共的视图放到Shared

public class HomeController : Controller
{
    public ActionResult Index()  // 视图 Views\Home\Index.cshtml
    {
    return View(); 
    }
    
    public ActionResult Get() // 视图 Views\Home\Get.cshtml
    {
        return View();
    }
    
}


Razor引擎

@字符是Razor中一个重要的符号,它被定义为Razor服务器代码块的开始符号

Razor用法
1.输出单一变量
视图 Views\Home\Index.cshtml 中
--输出当前时间
<div>
    @DateTime.Now
</div>

2.输出三元运算符
<div>
    @{ var i = 3;
    int[] nums = {1,4,6,7,3,8};
    }


    @(i == 3 ? "ok" : "no")
    
    @if(i == 4)
    {
        @:启动
    }
    else
    {
        @:停止
    }
    
    @foreach(var item in nums)
    {
        @item
    }

</div>

3.执行多行C#代码

4.Razor中注释

5.IF判断表达式

6.在程序区块中插入文字内容

7.foreach循环


MVC传值


传值方式:
1.Viewdata
2.Viewbag
3.TempData
4.Model(强类型传值)

--新建ASP.NET MVC 4 Web应用程序(MVC传值)--基本 --Controllers文件夹添加HomeController.cs
--添加Index方法对应的视图

public ActionResult Index()
{
    Session["key"] = "hello";
    String s = Session["key"]; //取值
    
    ViewData["key"] = "hello";
    ViewData["key"] //取值
    
    ViewBag.key = "hi";
    
    TempData["key"] = "hi";
    
    return View();

}


<body>
    <div>
        @ViewData["key"]
        
        @ViewBag.key
        
        @TempData["key"]
        

    </div>
</body>


Viewdata和Viewbag

添加一个类 Student.cs
public class Student
{
    public int Sid{get;set;}
    public string Sname{get;set;}
    public int Age{get; set;}
    
    public string sex{ get;set;}
    public string inte { get; set;}

}


public ActionResult Demo
{
    Student stu = new Student();
    stu.Sid = 5;
    stu.Sname = "liming";
    stu.Age = 18;
    
    ViewData["stu"] = stu;
    ViewBag.stu = stu;

    
    return View();
}

<div>
    @{
        @ViewData["stu"] as MVC传值.Models.Student //类型转换

        MVC传值.Models.Student viewdataStu = (MVC传值.Models.Student) @ViewData["stu"];
        
        MVC传值.Models.Student viewbagStu = ViewBag.stu;
    }
    
    @viewbagStu.Sname
    
    @viewdataStu.Sname
</div>

当传过来的数据是强类型数据的时候,viewdata需要作数据类型转换,viewbag不需要

传基本数据类型 viewdata与viewbag没有区别


viewdata和tempdata

public ActionResult Index()
{
    ViewData["abc"] = "abc";
    return RedirectToAction("getValue");//返回到一个方法
}
public ActionResult getValue() //添加一个视图
{
    return View();
    //return ViewData["abc"].ToString(); 报错
}
//在视图getValue中无法获得ViewData["abc"]的值


public ActionResult Index()
{
    TempData["abc"] = "abc";
    return RedirectToAction("getValue");//返回到一个方法
}
public ActionResult getValue() //添加一个视图
{
    return View();
    //return TempData["abc"].ToString(); 
}
//在视图getValue中可以获得TempData["abc"]的值


通过ViewData传值时不可以跨方法,TempData可以跨方法传值


强类型传值


public ActionResult ModelDemo() //添加一个视图,勾选创建强类型视图,模型类选择Student类
{
    Student stu = new Student();
    stu.Sid = 1;
    stu.Sname = "huahua";
    stu.Age = 20;
    
    return View(stu); //通过强类型传值
}

//ModelDemo.cshtml中
@modelodel MVC传值.Models.Student //表示传过来的为Student类型的对象
@Model List<MVC传值.Models.Student>  //表示传过来的为Student类型对象的集合
<div>
    @Model.Sname
</div>


request传值

<div>
    <form action = "/formDemo/getStudent" method="post">  //提交到formDemoController控制器的getStudent方法
        <table>
            <tr><td>学生姓名:</td><td><input type = "text" name = "Sname"/></td></tr>
            <tr><td>学生性别:</td><td><input type = "radio" name = "sex" value = "男"/>&nbsp&nbsp 
                                        <input type = "radio" name = "sex" value = "女"/></td> </tr>
            
            <tr><td>兴趣:</td> <td><input type = "checkbox" name = "inte" value="乒乓球"/>乒乓球
                                    <input type = "checkbox" name = "inte" value="羽毛球"/>羽毛球
                                    <input type = "checkbox" name = "inte" value="网球"/>网球
                                    <input type = "checkbox" name = "inte" value="游泳"/>游泳
                </td></tr>
            <tr><td><input type = "submit" value = "提交"/></td></tr>
        </table>
    </form>

</div>


public class formDemoController : Controller
{
    public ActionResult Index() //创建视图
    {
    
        return View();
    }
    
    public ActionResult getStudent() //使用request获取表单的值
    {
        string Sname = Request["Sname"];
        string sex = Request["sex"];
        string inte = Request["inte"];
        
        return "姓名:" + Sname +" 性别:" + sex + "兴趣:" + inte;
    }
    
    //通过参数获取表单提交过来的数据
    public ActionResult getStudentDemo1(string Sname,string sex)
    {
        return "姓名:" + Sname +" 性别:" + sex + "兴趣:" + Request["inte"];
    }

    
}


对象传值

<div>
    <form action = "/formDemo/getStudentDemo2" method="post">  //提交到formDemoController控制器的getStudentDemo2方法
        <table>
            <tr><td>学生姓名:</td><td><input type = "text" name = "Sname"/></td></tr>
            <tr><td>学生性别:</td><td><input type = "radio" name = "sex" value = "男"/>&nbsp&nbsp 
                                        <input type = "radio" name = "sex" value = "女"/></td> </tr>
            
            <tr><td>兴趣:</td> <td><input type = "checkbox" name = "inte" value="乒乓球"/>乒乓球
                                    <input type = "checkbox" name = "inte" value="羽毛球"/>羽毛球
                                    <input type = "checkbox" name = "inte" value="网球"/>网球
                                    <input type = "checkbox" name = "inte" value="游泳"/>游泳
                </td></tr>
            <tr><td><input type = "submit" value = "提交"/></td></tr>
        </table>
    </form>

</div>


public class formDemoController : Controller
{
    public ActionResult Index() //创建视图
    {
    
        return View();
    }
    
    public ActionResult getStudent() //使用request获取表单的值
    {
        string Sname = Request["Sname"];
        string sex = Request["sex"];
        string inte = Request["inte"];
        
        return "姓名:" + Sname +" 性别:" + sex + "兴趣:" + inte;
    }
    
    //通过参数获取表单提交过来的数据
    public ActionResult getStudentDemo1(string Sname,string sex)
    {
        return "姓名:" + Sname +" 性别:" + sex + "兴趣:" + Request["inte"];
    }

    //通过对象
    public string getStudentDemo2(Student stu)
    {
        stu.inte = Request["inte"];
        //要求:表单input标签name值均一一对应属性值,点击提交,自动拼接对象,并将各值赋值到对象的属性中
        return "姓名:" + stu.Sname  +" 性别:" + stu.sex + "兴趣:" + stu.inte;
        //通过这种方式复选框传不全,所以仍需要用Request传值方式
    
    }
}


formcollection传值


<div>
    <form action = "/formDemo/getStudentDemo3" method="post">  //提交到formDemoController控制器的getStudentDemo3方法
        <table>
            <tr><td>学生姓名:</td><td><input type = "text" name = "Sname"/></td></tr>
            <tr><td>学生性别:</td><td><input type = "radio" name = "sex" value = "男"/>&nbsp&nbsp 
                                        <input type = "radio" name = "sex" value = "女"/></td> </tr>
            
            <tr><td>兴趣:</td> <td><input type = "checkbox" name = "inte" value="乒乓球"/>乒乓球
                                    <input type = "checkbox" name = "inte" value="羽毛球"/>羽毛球
                                    <input type = "checkbox" name = "inte" value="网球"/>网球
                                    <input type = "checkbox" name = "inte" value="游泳"/>游泳
                </td></tr>
            <tr><td><input type = "submit" value = "提交"/></td></tr>
        </table>
    </form>

</div>
public class formDemoController : Controller
{
    public ActionResult Index() //创建视图
    {
    
        return View();
    }
    
    public ActionResult getStudent() //使用request获取表单的值
    {
        string Sname = Request["Sname"];
        string sex = Request["sex"];
        string inte = Request["inte"];
        
        return "姓名:" + Sname +" 性别:" + sex + "兴趣:" + inte;
    }
    
    //通过参数获取表单提交过来的数据
    public ActionResult getStudentDemo1(string Sname,string sex)
    {
        return "姓名:" + Sname +" 性别:" + sex + "兴趣:" + Request["inte"];
    }

    //通过对象
    public string getStudentDemo2(Student stu)
    {
        stu.inte = Request["inte"];
        //要求:表单input标签name值均一一对应属性值,点击提交,自动拼接对象,并将各值赋值到对象的属性中
        return "姓名:" + stu.Sname  +" 性别:" + stu.sex + "兴趣:" + stu.inte;
        //通过这种方式复选框传不全,所以仍需要用Request传值方式
    
    }
    //通过formcollection传值
    public string getStudentDemo3(FormCollection col)
    {
        return "姓名:" + col["Sname"]  +" 性别:" + col["sex"] + "兴趣:" + col["inte"];  //可以直接获取到复选框的值
    }
}

 

 

 

 

posted @ 2020-06-04 13:03  highlightyys  阅读(14)  评论(0编辑  收藏  举报