首页 新闻 会员 周边

MVC新手问题,关于ActionResult的参数

0
悬赏园豆:20 [已解决问题] 解决于 2015-01-19 11:04

一个登陆页面,在view的代码如下

@model shw2013.Models.adminLogin

@{
Layout = null;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div id="loginBox">
<div class="formBox">
@using (Html.BeginForm("checkAdminLogin", "Account", FormMethod.Post, new { name = "form1", id = "form1" }))
{
<div class="sft1">
<ul>
<li>@Html.TextBoxFor(m => m.userAc)</li>
<li>@Html.PasswordFor(m => m.userPwd)</li>
<li><span style="float:left; width:75px; overflow:hidden;">@Html.TextBoxFor(m => m.userYZM, new { style = "width:58px;" })</span><span style="float:left;"><img style="width:38px; height:20px;" id="Logon-vcode" onclick="javascript:this.src='/Account/Vcode?id=Logon&time=' + (new Date()).getTime();" src="@Url.Action("Vcode", "Account", new { id = "Logon" })" /></span></li>
</ul>
</div>
<div style="float:left;">
<a style="width:70px; margin-top:45px; height:35px;float:left;" href="#" onclick="javascript:document.form1.submit();"></a>
</div>
}
</div>
</div>
</body>
</html>

 

 

在Controllers对应的代码如下

public ActionResult checkAdminLogin(Models.adminLogin model)
{
if (ModelState.IsValid)
{
if ((string)Session["Logon"] == model.userYZM)
{
userInfo user = _userInfoDAL.getLoginUser(TextHelper.Html.InputText(TextHelper.Html.InputText(model.userAc,100), 30), TextHelper.Html.MD5(model.userPwd));
if (string.IsNullOrWhiteSpace(user.userMail))
{
return Content("<script>alert('用户名或密码错误!');window.location.href='/Account/superLogin/';</script>");
}
else
{
//如果不是管理员帐号一样提示错误
if (_userTypeDAL.getById(user.userType).TypeName != "管理员")
{
return Content("<script>alert('" + _userTypeDAL.getById(user.userType).TypeName + "不允许登录!');window.location.href='/Account/superLogin/';</script>");
}
else
{

FormsAuthentication.SetAuthCookie("shwilynn", false);//保存票据
//添加登录日志
Log.Txt.add(model.userAc + "从IP" + Request.UserHostAddress + "登录了后台");
//存用户名 //存用户Id
TextHelper.Html.setCookie("userId", user.Id.ToString());
return RedirectToAction("ilynn", "Admin");
}
}
}
else
{
return Content("<script>alert('验证码错误!');window.location.href='/Account/superLogin/';</script>");
}
}
else
{
return Content("<script>alert('输入数据不完整!');window.location.href='/Account/superLogin/';</script>");
}
}

 

 

这个是验证登陆的,那么问题来了, checkAdminLogin有一个model参数,而表单提交的action="/Account/checkAdminLogin",并没有一个地方传递model,是通过什么机制将输入框输入的信息转换为model对象并作为参数传递的呢?

ludi的主页 ludi | 初学一级 | 园豆:5
提问于:2015-01-06 17:13
< >
分享
最佳答案
0

@using (Html.BeginForm("checkAdminLogin", "Account", FormMethod.Post, new { name = "form1", id = "form1" }))

是这个,你不是包含在表单里面了吗,

@Html.TextBoxFor(m => m.userAc)</li>

@Html.PasswordFor(m => m.userPwd)

里面的文本值都是一一对应上你的实体类字段的,controller里面方法checkAdminLogin(Models.adminLogin model)参数里面自然就有数据带过来了。

收获园豆:20
KingMi | 小虾三级 |园豆:1344 | 2015-01-06 17:32
其他回答(2)
0

你F12看看生成的html脚本,标签的name值,就是model的属性。其实就是不用你写Resquest["XXX"]的一个简化。

凡一二三 | 园豆:85 (初学一级) | 2015-01-06 17:43
0

这就是Model Binding 机制 详细可以参考 http://www.cnblogs.com/willick/p/3424188.html

隔壁老王来了 | 园豆:99 (初学一级) | 2015-01-06 19:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册