Asp.net MVC学习之路-001

作者: 垃圾简书_吃枣药丸 | 来源:发表于2017-03-07 20:41 被阅读144次

    日期:17-3-7

    控制器三个约定:

    1. 控制器命名规范:"NameController",以Controller结尾
    2. 控制器必须是非静态类
    3. 实现IController接口(多次继承)
      控制器里面的方法都被称为Action
      Views文件夹下面会根据Controller名新建若干个以Controller命名的文件夹,该文件夹下还能,只能新建一个文件夹“Shared”,以及一个系统生成的web.config。

    添加视图:在Controller的方法名上右击添加视图

    WebForm与Asp.net MVC请求页面的区别:

    <b>webForm</b>请求的是一个aspx页面。 http://localhost/a.aspx

    <b>Asp.net MVC</b> 请求地址,请求控制器里面的方法。http://localhost/Home/index
    用户 > Controller-Action > ViewData数据-View
    Action:

    1. 处理用户的请求,Request,Response
    2. 调用业务逻辑(Model BLL DAL)
    3. 把数据传递给View进行展示

    ViewData[]从Controller向View传递数据
    Action 如果没有指定(return View("index");)对应的视图来展示数据的话,默认是寻找跟Action同名的View进行展示。一般Action名与指定的视图同名。

    前台表单代码

    <form action="/UserInfo/ProcessUserInfo" method="post">
                    <div class="form-group">
                        <label for="UserName">UserName</label>
                        <input type="text" class="form-control" name="txtName" id="UserName" placeholder="请输入用户名">
                    </div>
                    <div class="form-group">
                        <label for="Pwd">Password</label>
                        <input type="password" class="form-control" name="txtPwd" id="Pwd" placeholder="请输入密码">
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>
    

    从前台获取数据的四种方式

    方式一

    public ActionResult ProcessUserInfo()
            {
             string UserName = Request["txtName"];//与前台name同名。对于表单form,只有设置了 name 属性的表单元素才能在提交表单时传递它们的值。
                    string UserPwd = Request["txtPwd"];
            return Content("OK" + "</br>" + UserName + "</br>" + UserPwd);
    }
    

    方式二

     public ActionResult ProcessUserInfo(FormCollection collection)
            {
    string str = collection["txtName"];
    string pwd=collection["txtPwd"];
    return Content("OK" + "</br>" + str+ "</br>" + pwd
    }
    

    方式三

    public ActionResult ProcessUserInfo(string txtName,string txtPwd)//与前台name同名
    {
        return Content("OK" + "</br>" + txtName + "</br>" + txtPwd)
    }
    

    方式四

    public class Info
            {
                public string txtName { get; set; } //与前台name同名
                public string txtPwd { get; set; }
            }
    public ActionResult ProcessUserInfo(Info userA)
            {
            return Content("OK" + "</br>" + userA.txtName,string + "</br>" + userA.txtPwd)
    }
    

    HtmlHelper

    超链接的三种形式

    超链接方式1

            <a href="/Home/About">链接到About页面</a>
            <br/>
    

    超链接方式2

            这种方式避免了上面更改路由机制之后要更改所有的链接代码
            <br/>
            <a href=@Url.Action("About","Home")>链接到About</a>
            <br/>
    

    超链接方式3

            <br/>
            @Html.ActionLink("About页面","About","Home",null,new { style = "Color:green" ,@class="aaa"})  设置htmlAttr
    

    相关文章

      网友评论

        本文标题:Asp.net MVC学习之路-001

        本文链接:https://www.haomeiwen.com/subject/mahvgttx.html