2013-05-29 61 views
4

我是新来的asp.net mvc。参考文献this。我使用Tuples将两个模型发送到一个视图。我正在创建两个表单和两个提交按钮; 但值来空当服务器如何发布元组中的一个项目在asp.net mvc

@model Tuple<DAL.Models.LoginModel, DAL.Models.ForgotPassword>

@{ ViewBag.Title = "Login"; }

@using (Html.BeginForm("Login", "User", FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl })) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 

     <ol> 
      <li> 
       @Html.LabelFor(m => m.Item1.EmailID) 
       @Html.TextBoxFor(m => m.Item1.EmailID, new { @id = "txt_login_EmailID" }) 
       @Html.ValidationMessageFor(m => m.Item1.EmailID) 
      </li> 
      <li> 
       @Html.LabelFor(m => m.Item1.Password) 
       @Html.PasswordFor(m => m.Item1.Password) 
       @Html.ValidationMessageFor(m => m.Item1.Password) 
      </li> 

      <li> 
       <input type="submit" value="Login" id="btn_login" /> 

       @Html.CheckBoxFor(m => m.Item1.RememberMe) 
       @Html.LabelFor(m => m.Item1.RememberMe, new { @class = "checkbox" }) 
      </li> 
     </ol> 

    </fieldset> 
} 

在同一页面中另一种形式

@using (Html.BeginForm("ForgotPassword", "user")) 

    { 

    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 

     <ol> 
      <li> 
       @Html.LabelFor(m => m.Item2.EmailId) 
       @Html.TextBoxFor(m => m.Item2.EmailId, new { @id = "txt_fg_pwd_EmailID" }) 
       @Html.ValidationMessageFor(m => m.Item2.EmailId) 

      </li> 
      <li> 

       <input type="submit" value="Reset Password" /> 
      </li> 
     </ol> 
    </fieldset> 
} 

我的帖子的方法是

[HttpPost] 
    public ActionResult Login(LoginModel LoginUser , string returnUrl) 
    { 


     if (LoginUser.IsValid) 
     { 

      SetAuthenticationCookie(LoginUser, LoginUser.RememberMe); 

      return RedirectToLocal(returnUrl); 

     } 
     else 
     { 
      ModelState.AddModelError("", "Incorrect user name or password ."); 
      return View(LoginUser); 
     } 


    } 

`

任何修改都需要查看或POST方法

回答

18

尝试这样的:

[HttpPost] 
public ActionResult Login([Bind(Prefix = "Item1")] LoginModel loginUser, string returnUrl) 
{ 
    ... 
} 

因为你的价值观与Item1前缀,你应该表明该模型粘合剂。当然,您的ForgotPassword行动也是如此:

[HttpPost] 
public ActionResult ForgotPassword([Bind(Prefix = "Item2")] ForgotPassword model) 
{ 
    ... 
} 
+0

它现在工作的罚款。但我想知道为什么值变为空 – Kartheek

+3

因为你的输入字段被命名为'name =“Item1.EmailID”'?注意'Item1'前缀?那是因为你使用的助手:'@ Html.TextBoxFor(m => m.Item1.EmailID,new {@id =“txt_login_EmailID”})'。但是你的'LoginModel'没有一个名为'Item1.EmailID'的属性。它有一个名为'EmailID'的属性。不幸的是,在POST请求有效载荷中没有发送这样的值,并且默认的模型联编程序无法知道(除非你告诉他)他应该将请求主体中的Item1.EmailID值绑定到“EmailID”属性你的模型。模型活页夹按照惯例工作 –

+0

感谢您的解释。我现在明白了。 – Kartheek

1

这是错误的方式去做这件事。使用童工的行为:

控制器

[ChildActionOnly] 
public ActionResult LoginForm(string returnUrl) 
{ 
    ViewBag.returnUrl = returnUrl; 
    return View(new LoginModel()); 
} 

[ChildActionOnly] 
public ActionResult ForgotPasswordForm(string returnUrl) 
{ 
    ViewBag.returnUrl = returnUrl; 
    return View(new ForgotPassword()); 
} 

然后创建为每一个(在这里您可以只在每个现有表单视图代码复制视图只记得设置合适的型号。视图和关闭布局:

LoginForm.cshtml

@model DAL.Models.LoginModel 
@{ Layout = null; } 

<!-- Form code here --> 

ForgoutPasswordForm.cshtml

@model DAL.Models.ForgotPassword 
@{ Layout = null; } 

<!-- Form code here --> 

最后,在你原来的观点:

@{ Html.RenderAction("LoginForm", new { returnUrl = ViewBag.returnUrl }); } 

@{ Html.RenderAction("ForgotPasswordForm", new { returnUrl = ViewBag.returnUrl }); } 
+0

它似乎是我在错误的方式。我纠正了它,现在工作得很好。感谢路线图 – Kartheek

相关问题