Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post one item from tuples in asp.net mvc

I am new to asp.net mvc. with this reference this. i am using Tuples to send two models in to one view. i am creating two forms and two submit buttons ; but values are coming to null when it the server

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

this another form in the same page

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

my post method is

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


    }

`

Any changes need to to view or post method

like image 388
Kartheek Avatar asked May 29 '13 15:05

Kartheek


1 Answers

Try like this:

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

Since your values are prefixed with Item1 you should indicate that to the model binder. Of course the same stands true for your ForgotPassword action:

[HttpPost]
public ActionResult ForgotPassword([Bind(Prefix = "Item2")] ForgotPassword model)
{
    ...
}
like image 152
Darin Dimitrov Avatar answered Oct 03 '22 06:10

Darin Dimitrov