5

我是 asp.net mvc 的新手。有了这个参考this。我正在使用元组将两个模型发送到一个视图中。我正在创建两个表单和两个提交按钮;但是当它是服务器时,值将变为空

@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);
        }


    }

`

任何更改都需要查看或发布方法

4

2 回答 2

23

试试这样:

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

由于您的值以您为前缀,Item1因此您应该向模型绑定器表明这一点。当然,您的ForgotPassword操作也是如此:

[HttpPost]
public ActionResult ForgotPassword([Bind(Prefix = "Item2")] ForgotPassword model)
{
    ...
}
于 2013-05-29T15:18:13.713 回答
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());
}

然后为每个视图创建一个视图(您只需在其中复制每个视图的现有表单视图代码。请记住为视图设置正确的模型并关闭布局:

登录表单.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 }); }
于 2013-05-29T15:21:55.100 回答