如何在MVC3中从一个视图转到另一个视图

时间:2012-10-19 08:20:43

标签: asp.net-mvc-3

我还在学习,不知道如何做到这一点,我在我的控制器中:

[HttpGet]
public ActionResult Detail(int userId)
{
  var user = ZincService.GetUserForId(userId);
  UserDetailViewModel userDetail = new UserDetailViewModel();
  userDetail.UserId = userId;
  userDetail.Email = user.Email;
  userDetail.Firstname = user.Firstname;
  userDetail.Surname = user.Surname;
  return View(userDetail);
}

[HttpGet]
public ActionResult ChangeEmailAddress()
{
  return View();
}

在我的详细信息视图中:

  <div class="section _100">
     <%: Html.LabelFor(model => model.Email)%> 
         <div>
             <%: Model.Email  %>
             <%: Model.UserId %>
         </div>
         <div>                                      
             <%: Html.ActionLink("Change Email Address",  "ChangeEmailAddress", "User", new { area = "Admin", @id = Model.UserId, @email = Model.Email, @name = Model.Firstname }, 0) %>  

             <%: Html.ValidationMessageFor(model => model.Email)%>    
         </div>
  </div>

我只是想从视图中转到另一个视图(ChangeEmailAddress),用户实际上可以将他的电子邮件从旧更改为新文件?

谢谢

1 个答案:

答案 0 :(得分:0)

为什么你把0作为ActionLink的最后一个参数?它应该为空。

<%= Html.ActionLink(
    "Change Email Address",  
    "ChangeEmailAddress", 
    "User", 
    new { 
        area = "Admin", 
        id = Model.UserId, 
        email = Model.Email, 
        name = Model.Firstname 
    }, 
    null
) %>  

现在,您的ChangeEmailAddress操作可以将模型作为参数:

[HttpGet]
public ActionResult ChangeEmailAddress(UserDetailViewModel user)
{
    return View(user);
}

然后,您应该定义相应的ChangeEmailAddress.aspx视图,该视图将由此控制器操作呈现并强烈输入到您的视图模型。

但更好的方法是在请求中仅发送用户ID,然后使用此id检索实际的用户对象:

<%= Html.ActionLink(
    "Change Email Address",  
    "ChangeEmailAddress", 
    "User", 
    new { 
        area = "Admin", 
        id = Model.UserId, 
    }, 
    null
) %>  

然后:

[HttpGet]
public ActionResult ChangeEmailAddress(int id)
{
    // retrieve the user object using the id
    ...
}