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?
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;
}
}
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;
}
}
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;
}
}
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 :)
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.
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)
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.
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
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
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 :)
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.
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:
I have a controller that is going to handle the post
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?
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 :)
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.
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
in my model, anything wrong with that?
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
in my model, anything wrong with that?
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
in my model, anything wrong with that?
ooops, triple post...
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 :)
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
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
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.
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.
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
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 :)
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?
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 :)
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
is working on a reply...