0

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.

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

OK, I am now on the right page. The answer is I shouldn't have been doing it in the view. I have just added AccountToCreate.agency = User.Identity.Name; to my controller code and it now works a treat, sorry it took some time for me to get my head around it, easy when you know how!!
0

one of the fields in view?

how about simply setting

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

and then in View use it wherever you want.

2 Comments

Will try and explain a bit more.
Rob has explained in much more detail in his answer what I wanted to say.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.