Separating two forms in one view in ASP.Net MVC

I combined the creation account view and the log view in the same view. So this is a view with two forms, but they mix when I submit. If I try to log in and an error appears there, which is displayed with:

Html.ValidationSummary()

both forms get an error. And I started to rename the fields in loginPassword, createPassword, because otherwise, when I send and the password is missing, it is marked as missing on both sides.

How can these two forms be separated so that they can work on the same presentation / page?

+5
source share
5 answers

. , , ValidationSummary(). :

  • , . , , div, . Mahalo.
  • - ViewData, , . ValidationSummary, , ViewData.

.

№ 1, , . , , , № 2.

+3

, . , , ViewData, , ValidationSummary.

, , , , , , .

, tvanfosson, "EntryPageModel".

- Html.MyValidationSummary

<% using(Html.BeginForm("NewAccount", "Account")) %>
<% { %>
    <%= Html.MyValidationSummary("NewAccountForm") %>

    <%= Html.TextBox("NewAccount.FirstName") %>
    <%= Html.TextBox("NewAccount.LastName") %>
    <%= Html.TextBox("NewAccount.Email") %>
    <%= Html.Password("NewAccount.Password") %>
    <%= Html.Password("NewAccount.ConfirmPassword") %>
<% } %>

<% using(Html.BeginForm("Login", "Account")) %>
<% { %>
    <%= Html.MyValidationSummary("LoginForm") %>

    <%= Html.TextBox("Login.Email") %>
    <%= Html.Password("Login.Password") %>
<% } %>

- ViewData [ "Form" ]

public class Account : Controller
{
    private EntryPageModel _viewModel;

    public ActionResult NewAccount(FormCollection formValues)
    {
        try
        {
            //binding and validation for _viewModel.NewAccount
        }
        catch
        {
            ViewData["PostedForm"] = "NewAccountForm";
            return View("RegisterAndLogin", _viewModel);
        }
    }

    public ActionResult Login(FormCollection formValues)
    {
        try
        {
            //binding and validation for _viewModel.Login
        }
        catch
        {
            ViewData["PostedForm"] = "LoginForm";
            return View("RegisterAndLogin", _viewModel); //You'll want to pass in a model
        }
    }
}

html

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static string MyValidationSummary(this HtmlHelper html, string formName)
        {
            if (!string.IsNullOrEmpty(html.ViewData["PostedForm"])
                && (html.ViewData["PostedForm"] == formName))
            {
                return html.ValidationSummary();
            }

            return "";
        }
    }
}

HTHS,

+5

/, . , , . , , , .

, , - ( , ):

<%= Html.TextBox( "Login.Name" ) %>
<%= Html.TextBox( "Login.Password" ) %>


<%= Html.TextBox( "NewAccount.Name" ) %>
<%= Html.TextBox( "NewAccount.Password" ) %>
<%= Html.TextBox( "NewAccount.ConfirmPassword" ) %>

public ActionResult Login( [Bind(Prefix="Login")]AccountModel model )
{
    ...
}

:

public class AccountModel
{
      public string Name { get; set; }
      public string Password { get; set; }
      public string ConfirmPassword { get; set; }
}

public class EntryPageModel
{
     public AccountModel Login { get; set; }
     public AccountModel NewAccount { get; set; }
}
+2

If the forms submit for completely different actions, then your ModelStateDictionary should only contain errors that were caused by the action that was caused.

Can you post the appropriate code?

0
source

I am not sure if there is a way to split ValidationSummary ().

For your forms, you can create classes of models with which you would be attached, with various fields. However, this would not bring you much in favor of what you already have.

0
source

All Articles