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

I am new to asp.net mvc. with this link. I use Tuples to send two models in one view. I create two forms and two submit buttons; but the values ​​are close to zero when 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 other view on 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 sending method

  [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 must be viewed or submitted.

+4
source share
2 answers

Try it like this:

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

Since your values ​​are prefixed with Item1 , you must point this to the model binding. Of course, the same is true for your ForgotPassword action:

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

This is the wrong way. Use child actions:

controller

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

Then you create a view for each of them (where you simply copy the form view for each into your existing code. Remember to set the appropriate model for the view and disable the layout:

LoginForm.cshtml

 @model DAL.Models.LoginModel @{ Layout = null; } <!-- Form code here --> 

ForgoutPasswordForm.cshtml

 @model DAL.Models.ForgotPassword @{ Layout = null; } <!-- Form code here --> 

And finally, in its original form:

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

Source: https://habr.com/ru/post/1483380/


All Articles