我在学习MVC的过程中,遇见一个问题。首页我在Models层创建了一个类
public class Member
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "请输入用户名")]
[MaxLength(20, ErrorMessage = "名字不能超过20个字")]
public string UserName { get; set; }
[Required(ErrorMessage="请输入姓名")]
public string Name { get; set; }
[Required(ErrorMessage="请输入密码")]
[MaxLength(16,ErrorMessage="密码长度不得超过16个字")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required(ErrorMessage="请输入联系电话")]
[DataType(DataType.PhoneNumber,ErrorMessage="请输入正确的电话格式")]
public string PhoneNo { get; set; }
[Required(ErrorMessage="请选择所在部门")]
public string Department { get; set; }
public DateTime LoginOn { get; set; }
}
然后我在Controller层中创建了一个MemberController:
public class MemberController : Controller
{
//
// GET: /Member/
//注册页面
public ActionResult Register()
{
return View();
}
//写入登录信息
[HttpPost]
public ActionResult Register([Bind(Exclude="LoginOn")]Member member)
{
return View();
}
//注册会员信息页
public ActionResult MemberDetail(Member member)
{
return View();
}
//登录页面
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//执行登录
[HttpPost]
public ActionResult Login(string userName, string password)
{
if (ValidateUser(userName,password))
{
FormsAuthentication.SetAuthCookie(userName, false);
return RedirectToAction("MemberDetail", "Member");
}
ModelState.AddModelError("", "输入的账户或密码错误");
return RedirectToAction("Login", "Member");
}
//验证用户名与密码
private bool ValidateUser(string userName,string password)
{
throw new NotImplementedException();
}
//注销页面
public ActionResult Logout()
{
//清除Cookies中数据
FormsAuthentication.SignOut();
//清除Session
Session.Clear();
return RedirectToAction("Login", "Member");
}
}
MemberController控制器中具体的代码实现还没有完成。然后我用vs2013根据控制器生成了一个注册页面。
@model MvcWMSApplication.Models.Member
<h2>注册页</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Member</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PhoneNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PhoneNo)
@Html.ValidationMessageFor(model => model.PhoneNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Department)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Department)
@Html.ValidationMessageFor(model => model.Department)
</div>
<p>
<input type="submit" value="注册" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Login")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
但是我想实现在点击“注册”按钮后,跳转到MemberDetail,并且显示出刚才在注册页面中所输入的信息。请问应该如何实现这个功能?
页面之间传递数据的话,在你写输入信息文本框的时候添加一个name属性,在另一个页面用request.getParameter(“name”)就可以获取到值啦。
Visual studio 自带的新建项目里面的代码好好看看就有了。
首先,通常点击“注册”按钮后,注册信息(member detail)通常已经进行持久化(通俗点说叫保存到数据库了)。
所以MemberDetail页面只需要取数据库数据显示就可以,无需在页面间传递信息。