Why do you need to store the username in the view? You will surely be initiating the DB transaction from within a controller so if it's the username for the user that is currently logged in use the MembershipProvider as per the last suggestion:
HttpContext.Current.User.Identity.Name
If not perhaps you should consider creating a container/wrapper class that clearly represents your View model - some might consider this overkill for one extra property but I hate "magic strings" in code.
public class MyView
{
public string UserName { get; set; }
public MyObject MyMainObject { get; set;}
public MyView(string username, MyObject myMainObject)
{
this.Username = username;
this.MyMainObject = myMainObject;
}
}
then set your view model type as:
System.Web.Mvc.ViewPage<MyNamespace.MyView>
this then allows you to have strongly typed properties for everything in your view e.g.
<%=Model.Username %>
<%=Model.MyMainObject.Title %>
and in your controller you can parameterize your Action as
public ActionResult(MyMainObject myMainObject, string username)
{
//Do something here
//if not correct
return View(new MyView(username, myMainObject));
}
If instead you wanted to go down this path:
ViewData["Name"] = User.Identity.Name;
or
ViewData.Add("Name", User.Identity.Name);
Consider creating Enums to once again avoid using string literals e.g.
public enum UserEnum
{
Username,
Password
}
then use:
ViewData.Add(UserEnum.Username.ToString(), User.Identity.Name);