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 7056145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:49:58+00:00 2026-05-28T03:49:58+00:00

I have a _layout page which has a login box (partial view) and that

  • 0

I have a _layout page which has a login box (partial view) and that view has it’s own model. So the controller looks like this:

        public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(LoginModel loginModel)
    {
        if(ModelState.IsValid)
        {
            var g = new GallaryImage();
            var user = g.LoginUser(loginModel.Username, loginModel.Password);
            if(user != null) 
            {
                FormsAuthentication.SetAuthCookie(user.username, false);
                return RedirectToAction("Index", "Home");
            }
            ModelState.AddModelError("", "Invalid Username/Password");
        }
        return View(loginModel);
    }

But as soon as my main content page needs a model, my web app fails because the Login box expects a LoginModel type, but my content page is sending a different model:

This is the GET method for my main Index screen:

 public ActionResult Index()
    {
        IndexModel model = new IndexModel();
        var g = new GallaryService.GallaryImage();
        var i = g.GetRandomImage();

        if (i != null)
            model.RandomImageUrl = "~/Images/Watermarks/" + i.filename;
            return View(model);
    }

So, my main content page has an IndexModel, but my partial view has a LoginModel. When I try run it, I get an error:

“The model item passed into the dictionary is of type ‘GalleryPresentation.Models.IndexModel’, but this dictionary requires a model item of type ‘GalleryPresentation.Models.LoginModel’.”

How do I handle this – My _layout needs the model for the login box.

As requested, here is the Loginbox cshtml file.

    @using GalleryPresentation.Models
@model LoginModel

<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
@using (Html.BeginForm("index", "Account", FormMethod.Post))
{
    <table class="smallBox">
        <tr>
            <td>@Html.LabelFor(m => m.Username)</td>
            <td>@Html.TextBoxFor(m => m.Username, new { @class = "smallText" })</td>
            <td>@Html.LabelFor(m => m.Password)</td>
            <td>@Html.PasswordFor(m => m.Password, new { @class = "smallText" })</td>
        </tr>
        <tr>
            <td colspan="4" align="right"><input type="submit" value="Login"/></td>
        </tr>
        <tr>
            <td colspan="2">@Html.ValidationSummary()</td>
        </tr>
    </table>

}

And the Index.cshtml file (THe main content screen) has this:

    @using GalleryPresentation.Models
@model IndexModel
@{
    ViewBag.Title = "Craig and Melanie's Digital Moments";
}

<br/>
<div style="text-align: center">
    <img src="@Url.Content( Model.RandomImageUrl)" alt="@ViewBag.Title" />
</div>
  • 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-28T03:49:59+00:00Added an answer on May 28, 2026 at 3:49 am

    Questions like this aren’t always the easiest to answer because there isn’t a straightforward solution. There are several issue though that should be considered. If it is possible, I would recommend that you handle login validation errors in a separate view. The partial view for the small login box then does not require a strongly-typed view model.

    There’s no perfect solution, but I don’t think that it makes a lot of sense for you to always be creating LoginModel objects on every request that renders a view which depends on _Layout. The solution below advocates the creation of a separate login view which can be used for explicit login attempts and for the handling of any login failures.

    If you have any trouble following this, feel free to your question in a comment and I’ll do my best to answer.

    Login Box

    @using (Html.BeginForm("Index", "Account"))
    {
        <table class="smallBox">
            <tr>
                <td>Username</td>
                <td>@Html.TextBox("Username", new { @class = "smallText" })</td>
                <td>Password</td>
                <td>@Html.Password("Password", new { @class = "smallText" })</td>
            </tr>
            <tr>
                <td colspan="4" align="right"><input type="submit" value="Login"/></td>
            </tr>
        </table>
    }
    

    Account Controller

    public ActionResult Login()
    {
        return View();
    }
    
    public ActionResult RetryLogin()
    {
        ModelState.AddModelError(null, "The Username or Password you entered is invalid.  Please try again.");
        return View("Login");
    }
    
    [HttpPost]
    public ActionResult Index(LoginModel loginModel)
    {
        if(ModelState.IsValid)
        {
            var g = new GallaryImage();
            var user = g.LoginUser(loginModel.Username, loginModel.Password);
            if(user != null) 
            {
                FormsAuthentication.SetAuthCookie(user.username, false);
                return RedirectToAction("Index", "Home");
            }
    
            ModelState.AddModelError("", "Invalid Username/Password");
        }
    
        return RedirectToAction("RetryLogin");
    }
    

    Login View

    @using (Html.BeginForm("Index", "Account"))
    {
        @Html.ValidationSummary()
        <!-- login form here -->
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for some implementation advice: I have a page that has a 3-tab ajaxToolkit:TabContainer.
I have a method in my controller that handles generic page requests, meant for
I have a JSF page which has a variable number inputText elements containing numeric
Let's say we have a HTML page which has the structure as <html> <body>
I have a webpage which has content layout like 1,2,3 in markup (and also
I have a div in which a page is loaded with the DojoX Layout
I have a page layout for my MOSS '07 site that I want put
I have a page layout that I use in my SharePoint solution when I
I have a route like following, ideally I would like it to match: domain.com/layout/1-slug-is-the-name-of-the-page
I have a layout that is working, but it has one very annoying problem..

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.