Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4616284
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:57:35+00:00 2026-05-22T01:57:35+00:00

My task is create both login and registration form on one view! I have

  • 0

My task is create both login and registration form on one view!

I have home view. On a home view I have two forms, login and register. They rendered by @Html.RenderPartial(partial view name, model). That forms relates to Account controller (Login and Register actions).

Let me first provide you with some code…

Models:

// Login model, that contains email and password
public class Login
{
    …
}
// Registration model that contains minimum user data, e.g. name, surname
public class Register
{
    ...
}
// Model for home view which contains models for both register and login
public class Home
{
    public Login    LoginModel    { get; set; }
    public Register RegisterModel { get; set; }
}

Views:
Index.cshtml

@model App.Models.Home.Home
<!-- Login Box -->
@{ Html.RenderPartial("_LoginArea", Model.LoginModel); }
<!-- Register Box -->
@{ Html.RenderPartial("_RegisterArea", Model.RegisterModel); }

_LoginArea.cshtml

@model App.Models.Account.Login
<div style="float: left; margin-right: 100px;">
    <h1>@Localization.Account.Login</h1>
    <!-- Login form -->
    <div id="loginBox">
    @using (Html.BeginForm("Login", "Account", FormMethod.Post)) 
    {    
        <div class="editor-label">
        @Html.LabelFor(m => m.Email):
        </div>
        <div class="editor-field">
        @Html.TextBoxFor(m => m.Email)
        @Html.ValidationMessageFor(m => m.Email)
        </div>
        <div class="editor-label">
        @Html.LabelFor(m => m.Password):
        </div>
        <div class="editor-field">
        @Html.PasswordFor(m => m.Password)
        @Html.ValidationMessageFor(m => m.Password)
        </div>
        <div class="editor-label">
        @Html.CheckBoxFor(m => m.RememberMe)
        @Html.LabelFor(m => m.RememberMe)
        </div>
        <p>
            <input type="submit" value="@Localization.Account.Login" />
        </p>    
    }
    </div>
</div>

_RegisterArea.cshtml

@model App.Models.Account.Register
<div style="vertical-align: bottom;">
    <h1>@Localization.Account.Register</h1>
    <div>
    @using (Html.BeginForm("Register", "Account"))
    {
        //same things like in login
    } 
    ...

Controllers:

HomeController

//
// GET: /Home/

public ActionResult Index(Home model)
{
    //
    // If logedin redirect to profile page
    // Else show home page view
    //

    if (Request.IsAuthenticated)
    {
        return RedirectToAction("Index", "User", new { id = HttpContext.User.Identity.Name });
    }
    else
    {
        model.LoginModel = new Login();
        model.RegisterModel = new Register();
        return View(model);
    }
}

Just show you Login action from Account controller

// 
// POST: /Account/Login

[HttpPost]
public ActionResult LogIn(Login model)
{
    if (Request.IsAuthenticated)
    {
        return RedirectToAction("Index", "User", new { id = HttpContext.User.Identity.Name });
    }
    else
    {
        if (ModelState.IsValid)
        {
            if (model.ProcessLogin())
            {
                return RedirectToAction("Index", "User", new { id = HttpContext.Session["id"] });
            }
         }
     }

     //Note: this is problem part
     // If we got this far, something failed, redisplay form
     return View("~/Views/Home/Index.cshtml", model);
}

So everything works fine, BUT! When model state is not valid I have following exception

The model item passed into the dictionary is of type 'App.Models.Account.Login', but this dictionary requires a model item of type App.Models.Home.Home'.

I can bind that partial views and account actions to Home model, but this is not what I need.
I planning to use _LoginArea.cshtml view in other views (for example in a page header of ‘User’ view, and that view has another model)
So, I need action method (e.g. Login) to retrieve Login model, not Home or whatever else. But in this case I’m unable to return model to Home view.
How can I solve this issue? What is a best proper way?
Sorry for a lot of code, just want clear things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:57:36+00:00Added an answer on May 22, 2026 at 1:57 am

    SOLVED!!! Used Html.RenderAction instead.
    Here is code:

    Index.cshtml

    <!-- Login form -->
    @{ Html.RenderAction("Login", "Account"); }
    
    <!-- Register form -->
    @{ Html.RenderAction("Register", "Account"); }
    

    Partial views are same…

    Controller’s actions:

    //
    // GET: /Account/Login
    
    public ActionResult Login()
    {
        Login model = new Login();
    
        if (TempData.ContainsKey("Login"))
        {
             ModelStateDictionary externalModelState = (ModelStateDictionary)TempData["Login"];
             foreach (KeyValuePair<string, ModelState> valuePair in externalModelState)
             {
                 ModelState.Add(valuePair.Key, valuePair.Value);
             }
        }
    
        return View("_LoginHome", model);
    }
    
    // 
    // POST: /Account/Login
    
    [HttpPost]
    public ActionResult Login(Login model)
    {
        if (Request.IsAuthenticated)
        {
            return RedirectToAction("Index", "User", new { id = HttpContext.User.Identity.Name });
        }
        else
        {
            if (ModelState.IsValid)
            {
                if (model.ProcessLogin())
                {
                    return RedirectToAction("Index", "User", new { id = HttpContext.Session["id"] });
                }
            }                
        }
    
        TempData.Remove("Login");
        TempData.Add("Login", ModelState);
    
        // If we got this far, something failed, redisplay form
        return RedirectToAction("Index", "Home");
    }
    

    Same things for Register actions.

    As u can see I’m using TempData to store model state, and then retrieve it when action is begin render.
    Other question is how we can know where we should come back if model state is invalid. My solution is to parse Request.UrlReferrer, like ataddeini said.

    Big thanks to my friend Danylchuk Yaroslav for him help!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such task - to create control that union two controls (DataGrid from
I have used the jarbundler ant task to create an OSX (10.6.4) XXX.app for
I have big, fat and ugly legacy program. One task I had to accomplish
I have an algorithm where I create two bi-dimensional arrays like this: TYPE TPtrMatrixLine
I have a very complex task - create a software that imports XMl files
I have a task to create a decision tree for defining a person's creditworthiness
I have a static function in a class with two overloads. Both the overloads
I am trying to use the axis-java2wsdl ant task to create a wsdl from
I want to create a custom MSBuild task that changes my .cs files before
I need to create tasks in a SharePoint Task List with c#. Can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.