Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniël Tulp 33 posts 64 karma points
    Aug 06, 2013 @ 16:49
    Daniël Tulp
    0

    Access user id in controller in Umbraco 6.1.3 with MVC

    Hi,

    I am developing a custom application on Umbraco and I like what I get from Umbraco thus far, but now I am puzzled.

    I have a small form in a partial view like so:

    using (Html.BeginUmbracoForm<ProfielVragenController>("Index"))
    {
     <h5>Vraag 1: @profielvraag1.GetPropertyValue("profielVraag").ToString()</h5>
    <span>Mee oneens</span><span>Mee eens</span>
    @Html.RadioButton("Antwoord", 1)
    @Html.RadioButton("Antwoord", 2)
    @Html.RadioButton("Antwoord", 3)
    @Html.RadioButton("Antwoord", 4)
    @Html.RadioButton("Antwoord", 5)
    @Html.Hidden("ProfielVraagID",profielvraag1.Id)
    @Html.Hidden("NextID", profielvraag2.Id)
    <button type="submit" class="btn btn-checklist">Volgende</button>
    }
    

    I have a controller that is going to handle the post

    public class ProfielVragenController : SurfaceController
    {
        [HttpPost]
        public ActionResult Index(ProfielVraag model)
    {    
        //model not valid, do not save, but return current umbraco page
        if (!ModelState.IsValid)
        {        
            return CurrentUmbracoPage();
        }
    
            if (string.IsNullOrEmpty(umbraco.library.RequestCookies("ConsToolUserGuid")))
            {
                Guid GastGuid = Guid.NewGuid();
                umbraco.library.setCookie("ConsToolUserGuid", GastGuid.ToString());
            }
    
            int user = UmbracoContext.UmbracoUser.Id;
            ViewBag.User = UmbracoContext.PageId;
    
            return RedirectToCurrentUmbracoPage();
        }
    }
    

    As you can see I'm trying to get the user ID of the currently logged in user. This shouldn't be hard, but every user or member object of Umbraco I try is Null. How can I do this?

  • Charles Afford 1163 posts 1709 karma points
    Aug 06, 2013 @ 20:49
    Charles Afford
    100

    What you are doing looks a little odd.  I would try and push your logic you have in the model is valid in to a class somewhere.

    I would not use the directToCurrentUmbracoPage() and CurrentPage() just use return partialview() return View() ect.

    You want to use the

    MembershipUser current = Membership.GetUser();

    This will get the current logged on user.

    Charlie :)

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 08:28
    Daniël Tulp
    0

    Charlie,

    you are right, this belongs in my model, will try solution and give credit.

    About RedirectToCurrentUmbracoPage() and CurrentPage(), I use this as it is written in http://our.umbraco.org/Documentation/Reference/Mvc/forms/turorial-partial-views if you feel differently, please discuss and amend the documentation

    Thanks.

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 09:15
    Daniël Tulp
    0

    Charlie,

    although this should give me the user if I have the username, what I want is the user id of the currently logged in user so I can place this in my DB table alongside the choices the user made so I can reference to his choices later on.

    I am now doing it with

    public int UmbracoUserID
        {
            get
            {
                int userid = UmbracoContext.Current.UmbracoUser.Id;
                return userid;
            }
        }
    

    in my model, anything wrong with that?

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 09:28
    Daniël Tulp
    0

    Charlie,

    although this should give me the user if I have the username, what I want is the user id of the currently logged in user so I can place this in my DB table alongside the choices the user made so I can reference to his choices later on.

    I am now doing it with and seems to work

    public int UmbracoUserID
        {
            get
            {
                int userid = UmbracoContext.Current.UmbracoUser.Id;
                return userid;
            }
        }
    

    in my model, anything wrong with that?

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 09:44
    Daniël Tulp
    0

    Charlie,

    although this should give me the user if I have the username, what I want is the user id of the currently logged in user so I can place this in my DB table alongside the choices the user made so I can reference to his choices later on.

    I am now doing it with and seems to work

    public int UmbracoUserID
        {
            get
            {
                int userid = UmbracoContext.Current.UmbracoUser.Id;
                return userid;
            }
        }
    

    in my model, anything wrong with that?

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 09:44
    Daniël Tulp
    0

    ooops, triple post...

  • Charles Afford 1163 posts 1709 karma points
    Aug 07, 2013 @ 10:16
    Charles Afford
    0

    No problem, out of intrest why do you need the UmbracoUser ID?

    The UmbracoUser = Umbraco back end

    The Member = Umbraco front end

    When you say refrence the choice later on?  What does this mean?  That fact that you are trying to bypass the underlying api and architecture  makes me think that there is a much simplier way of doing what you are trying to do :).  Charlie.

    The documentation is correct you can use that and it works.  In my exp i have never needed it prefering to use the standard returning to view or partial.  It simply means that you know what is going on :)

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 10:51
    Daniël Tulp
    0

    Charlie,

    thanks for wanting to help me out.

    What I am doing is as follows: Visitors to my website will go through a list of questions and will end up with a checklist of things relevant for their living/household needs. So I have created documenttypes for the questions (so that content can be maintained in Umbraco) and I want to store the answers given by the user in my database. As this is not basic Umbraco functionality I thought I'd write it myself.

    I want to store the user Id as I believe it to be the simplest way to get the answers given by the user when he/she visits the checklist page.

    If you think I should do this differently, more effectively, better in sync with Umbraco, please let me know.

    Edit: I think I know where the confusion comes from. UmbracoUserID is just a property name I made, it has no relevance to UmbracoUser. I'll rename to be sure I don't make the mistake someday myself.

    Cheers, Daniël

  • Charles Afford 1163 posts 1709 karma points
    Aug 07, 2013 @ 11:28
    Charles Afford
    0

    No!  The Vistors to your website will be members and not users.  Users refer to the backend of umbraco.

    Do you mean MEMBERS or USERS?  :).

    I am assuming the users will have to register to your site in order for you to store the information?

    If this is the case it would seem you would find it much easier by create a membertype with custom properties (set what your members answer against these)

    Also are you using webforms or MVC/?  Charlie

     

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 12:11
    Daniël Tulp
    0

    I'm using MVC and indeed I need to use members instead of users. I'm a Umbraco newby and this had slipped my attention.

    What do you mean by "set what your members answer against these"? How can I use member properties to store my data? Sounds like the wrong place to me.

  • Charles Afford 1163 posts 1709 karma points
    Aug 07, 2013 @ 13:03
    Charles Afford
    0

    I am assuming that the questionaire ia generic and a user needs to signed up.  In doing so they will answer these questions?

    Well all the member propeties are stored in the database and you can create custom properties on a member.  See: http://charlesafford.com/umbraco-membership-provider.aspx  

    Thus if you create some custom properties on a member and then on the checklist page for example you can simply set these answers against the custom properties you setup for the logged on member.

    Does that make sense?

    Charlie. 

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 13:19
    Daniël Tulp
    0

    Charlie,

    your page has a 'System.NullReferenceException: Object reference not set to an instance of an object' error.

    It makes some sense, but I'd rather store the values in one XML in a custom table to separate the tech from the content so our editors can and, change and add the questions in the Umbraco backend

  • Charles Afford 1163 posts 1709 karma points
    Aug 07, 2013 @ 13:29
    Charles Afford
    0

    No it does not?  I just went to it?.  Which browser?

    I dont see how you are solving any problems doing it your way?  Could you explain.  Charlie :)

  • Daniël Tulp 33 posts 64 karma points
    Aug 07, 2013 @ 13:58
    Daniël Tulp
    0

    You are being very patient with me, wow.

    The link seems to work now.

    What I think you are proposing is that I add the fields where the answers to the questions are placed as properties to the membertype. So every member would have his or her answers directly connected to their account.

    Is my understanding correct?

    The problem with this approach in my eyes is flexibility. What if I wanted users to have more then one checklist (more then one set of answers) I also foresee problems with our Team Foundation Server and the ability for other developers in our company to work with your approach as they would not have the membertype in their database that I have or changes to the membertype would mean that they have to update their database

    Do you think these concerns are valid?

  • Charles Afford 1163 posts 1709 karma points
    Aug 07, 2013 @ 14:17
    Charles Afford
    0

    To your first point, yes that is what i am suggesting.

    How do you foresee a problem with users wanting more than one check list?

    Yes it would mean having to update the database but this is the same as your approach.

    As for flexability, it just feels you are adding a whole additional layer of complication.  Putting the data in the database, matching it against a user, using some sort of store proc?  to get the data out again.  This could all be done much easier and simpler.  Only my opionon.  Charlie :) 

  • Daniël Tulp 33 posts 64 karma points
    Aug 12, 2013 @ 08:18
    Daniël Tulp
    0

    Charlie,

    thanks for your thinking with me. As I'm not (yet) a Umbraco rookie or better, I prefer to stick to things I know and things I know how to write unit tests for. So I'm sticking with the route I have taken, but will certainly check out all the possibilities with the membertypes.

    Cheers, Daniël

Please Sign in or register to post replies

Write your reply to:

Draft