Steve Newton
Steve Newton

Reputation: 1078

MVC - Default Value in Field

I have the following code in my controller:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create([Bind(Exclude = "Id")] BackupSet AccountToCreate)
    {
        if (!ModelState.IsValid)
            return View();

        _DBE.AddToBackupSet(AccountToCreate);
        _DBE.SaveChanges();

        return RedirectToAction("Index");
    }

I need to have the value of User.Identity.Name set to be the value of one of the fields in the create view when I post it to the database.

I am sure its very simple but really don't know how.

Thanks, Steve.

Upvotes: 0

Views: 949

Answers (3)

Rob
Rob

Reputation: 10248

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);

Upvotes: 1

ARM
ARM

Reputation: 2385

Short Answer:

HttpContext.Current.User.Identity.Name

Long Answer:

You should probably make a membership service to provide that value. The default MVC2 project will provide IMembershipService interface which you can expand to provide property: CurrentUserName (or whatever you like)

public string CurrentUserName
        {
            get
            {
                var context = HttpContext.Current;
                if (null != context)
                    return context.User.Identity.Name;

                var user = Thread.CurrentPrincipal;
                return (null == user)
                           ? string.Empty
                           : user.Identity.Name;
            }
        }

Upvotes: 0

neeebzz
neeebzz

Reputation: 11538

one of the fields in view?

how about simply setting

ViewData["Name"] = User.Identity.Name 

and then in View use it wherever you want.

Upvotes: 0

Related Questions