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

The Archive Base Latest Questions

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

I am developing an application where I need to pass the value of username

  • 0

I am developing an application where I need to pass the value of username from a controller to a view.
i tried ViewData as given in http://msdn.microsoft.com/en-us/library/system.web.mvc.viewdatadictionary.aspx

My code in controller is

public ActionResult Index(string UserName, string Password)
{
        ViewData["UserName"] = UserName;
        return View();
}

where username and password are obtained from another form.

And the code in the view is

@{
    ViewBag.Title = "Index";  
}
<h2>Index</h2>
<%= ViewData["UserName"] %>

But when I run this code, the display shows <%= ViewData[“UserName”] %> instead of the actual username say, for example “XYZ”.

How should I display the actual UserName?

Thank you very much in advance for your help.

  • 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-27T23:57:14+00:00Added an answer on May 27, 2026 at 11:57 pm

    You’re using razor syntax here but you’re trying to mix it with older asp.net syntax, use

    @ViewData["UserName"] 
    

    instead

    Also, normally you wouldn’t use the view bag to pass data to the view. Standard practice is to create a model (a standard class) with all of the bits of data your View (page) wants then pass that model to the view from your controller (return View(myModel);)

    To do this you also need to declare the type of model you’re using in your view

    @model Full.Namespace.To.Your.MyModel
    

    read http://msdn.microsoft.com/en-us/gg618479 for a basic mvc models tutorial

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

Sidebar

Related Questions

I'm developing an application. In it I need to pass an array from one
i am developing one application with map view i need display the weather depends
I am developing an application where I need to generate an Image from text
I'm developing Flickr client. It's a desktop application. I need to pass authentication without
I am developing an application were I need to transform XML documents that look
I'm developing a web application and I need to mix Forms & Windows authentication
I'm developing an application where I the need to invoke a method of a
I am developing an application in asp.net mvc where i need to have Chat
I am developing an application for proof of concept at school and I need
I'm developing an application through Core Data and I need to perform some calculation

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.