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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:14:24+00:00 2026-05-27T02:14:24+00:00

I have a single view that is used by two Controller Actions. My Controller:

  • 0

I have a single view that is used by two Controller Actions.

My Controller:

AccountController {
    ...
    ActionResult LogOn() {
        return View()
    }

    [HttpPost]
    ActionResult LogOn(LogOnModel model) {
        return View(model)
    }
}

My View:

<% if (!String.IsNullOrEmpty(Model.UserName)) { %>
    <div class="username"><% Response.Write(Model.UserName); %></div>
<% } %>

Of course, the problem with the above code is that I get an error “Object Reference not set to an instance of an object” on the line that references Model.UserName.

What is the best way to my view to support both Actions? I can replace the HttpGet LogOn method with:

ActionResult LogOn() {
    result View(new LogOnModel()); 
}

That works, but it doesn’t seem very elegant and I’m sure an empty model will break some more complex models that I will need to create later on. Is there a better way? Thanks!

  • 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-27T02:14:25+00:00Added an answer on May 27, 2026 at 2:14 am

    What is the best way to my view to support both Actions?

    If a view is strongly typed to a view model (which is what absolutely all your views should be) you should make sure that all your controller actions serving those views (which is pretty much all your controller actions in your application) are passing a view model to those views.

    Which is pretty much bla-bla which translated into source code pretty basically means this:

    ActionResult LogOn() {
        return View(new LogOnModel())
    }
    

    That works, but it doesn’t seem very elegant and I’m sure an empty
    model will break some more complex models that I will need to create
    later on.

    That’s the correct way. Don’t worry. You ain’t gonna break anything. It’s by not providing a view model to your views that you are breaking stuff.

    Also as a side note you should absolutely never use Response.Write in a view. So instead of:

    <% Response.Write(Model.UserName); %>
    

    you totally want:

    <%= Html.DisplayFor(x => x.UserName) %>
    

    or:

    <%: Model.UserName %>
    

    but personally I prefer the DisplayFor helper since it will take care of any metadata.

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

Sidebar

Related Questions

I have picker controls on a single view with two outlets for each. When
I have a single controller, Index , that manages several non-specialized informational pages (e.g
I have an abstract generic view-model that I use as a base-class for several
I have a WinForms application with a view where the user selects a single
I have an single threaded, embedded application that allocates and deallocates lots and lots
I have a single user java program that I would like to have store
I have a single line CEikLabel in my application that needs to scroll text.
I have a single spool mbox file that was created with evolution, containing a
I have a single Rails 2.2.2 app that I want to 'share' with multiple
I have a very simple web part. I have a single grid view, which

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.