先看一下效果
实现控制器内代码
public class UserInfoController : Controller
{
// GET: GuanLiXiaoXinXi/UserInfo
public ActionResult Index()
{
return View();
}
public ActionResult UserRegist()
{
return View();
}
public ActionResult ProcessUserRegist(string txtName,string txtPwd,ParaClass data)
{
//怎么拿到用户提交来的表单?
//第一种方法获取数据
// string str = Request["txtName"];
//第二种方法获取数据,前提是()里面加入以上参数、
//string str = collection["txtName"];
//第三种方法,之间在()里写参数 此时return 括号内的参数值也改变
//第四种方法增加ParaClass data 注意和增加下面的public
//Response.Write("ok" + str);
//Response.End();
//return Content("ok" + str); //往前台输出一个字符串,不需要视图
return Content("ok" + txtName);
}
public class ParaClass
{
public string txtName { get; set; }
public string txtPwd { get; set; }
}
}
视图页面代码
<body>
<div>
<form action="/UserInfo/ProcessUserRegist" method="post"> @*与控制器名称和视图名称一致*@
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="txtName" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="txtPwd" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</div>
</body>