MVC1.12

81 阅读1分钟

1.首先新建一个空白的MVC项目:,新建一个控制器Home

    #region 1.传统的方式:通过Qequest方式获取从视图传递过来的数据
    [HttpPost]
    public ActionResult PostIndex()
    {
        //1.传统的方式:通过Qequest方式获取从视图传递过来的数据
        //这里的Request["Name"]中的Name是文本框中的name属性值,
        //不能在视图中去掉,否则报错:未将对象引用设置到对象的实例
        //Request不能通过索引来取值,即Request[0]
        string name = Request["Name"].ToString();
        string sex = Request["Sex"].ToString();
        int age = Convert.ToInt32(Request["Age"].ToString());
        string phone = Request["Phone"].ToString();
        StringBuilder sb = new StringBuilder();
        sb.Append("<b>Name:" + name + "</b><br/>");
        sb.Append("<b>Sex:" + sex + "</b><br/>");
        sb.Append("<b>Age:" + age + "</b><br/>");
        sb.Append("<b>Phone:" + phone + "</b><br/>");
        return Content(sb.ToString());
    } 

对应的Index视图 @{ Layout = null; }

Index #parentDIV { border:1px solid green; text-align:center; } </style>
@using (Ajax.BeginForm("PostIndex", "Home", new AjaxOptions() { UpdateTargetId = "myDIV", HttpMethod = "POST" })) {
@*label中的for里面的值是相对应的文本框的ID值*@ 姓名:
性别: @*提交的是值value*@
年龄:
电话:
确定
}
```