【问题标题】:How to post one item from tuples in asp.net mvc如何在asp.net mvc中从元组中发布一项
【发布时间】:2013-05-29 15:12:10
【问题描述】:

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

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


    }

`

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

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    试试这样:

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

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

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

    【讨论】:

    • 现在工作正常。但我想知道为什么值变成空
    • 因为你的输入字段被命名为name="Item1.EmailID"?注意到Item1 前缀了吗?那是因为您使用的助手:@Html.TextBoxFor(m =&gt; m.Item1.EmailID, new { @id = "txt_login_EmailID" })。但是您的LoginModel 没有名为Item1.EmailID 的属性。它有一个名为EmailID 的属性。不幸的是,在 POST 请求有效负载中没有发送这样的值,并且默认模型绑定器无法知道(除非您告诉他)他应该将请求正文中的 Item1.EmailID 值绑定到模型的 EmailID 属性.模型绑定器按约定工作
    • 感谢您的解释。我现在明白了。
    • @DarinDimitrov 谢谢
    【解决方案2】:

    这是错误的做法。使用子操作:

    控制器

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

    【讨论】:

    • 看来我走错路了。我纠正了它。现在工作正常。感谢路线图
    猜你喜欢
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多