0

this is my controller action method-------

    public HttpResponseMessage PostPost(Post post)
    { 
     // previosly i was using simple membership to get id of logged in user
       // post.PostedBy = WebSecurity.CurrentUserId;
          post.PostedBy = User.Identity.GetUserId<int>();
           post.PostedDate = DateTime.UtcNow;
        ModelState.Remove("post.PostedBy");
        ModelState.Remove("post.PostedDate");
        if (ModelState.IsValid)
        {
            db.Posts.Add(post);
            db.SaveChanges();
   // now i am facing problem at this line. it is saying that
  //Operator '=='   cannot be applied to operands of type 'int' and 'string'  
            var usr = db.Users.FirstOrDefault(x => x.Id == post.PostedBy);

my code was working fine when i was using simple membership.what should i try to make it working. here, post is a class with property PostedBy as int and i want to assign current loggedIn userId value to this property.

duke
  • 1,816
  • 2
  • 18
  • 32
  • 2
    I love that you have used the word "Post" 20 times in a single question, especially the method signature :) – DavidG Oct 13 '15 at 12:55
  • hahaha what can i do ..... it's just a code i have not noticed that @DavidG – duke Oct 13 '15 at 13:03

1 Answers1

0

Check the answer in this link: How to get current user, and how to use User class in MVC5?

Check how the code is written in the answer:

using Microsoft.AspNet.Identity;

...

User.Identity.GetUserId();

if that didn't work please debug and trace the code and tell us what's the value of User.Identity ?

Community
  • 1
  • 1
Emad Khalil
  • 803
  • 1
  • 8
  • 14
  • i am facing problem at the last line where it gives red underline that == operands cannot be applied to int and string. I have seen all the links given by u but problem still persists – duke Oct 13 '15 at 13:06
  • sorry i didn't notice the comment written in your code, ok, for the table "Users" in the database which you are using here, what is the datatype of the column "Id" ? – Emad Khalil Oct 13 '15 at 13:13
  • Column Id is the default one provided by identity2.1. It is primary key with int datatype.I want to get this value and assign it to post table's PostedBy property. – duke Oct 13 '15 at 13:20
  • I think it is somehow using the Id as a string, would you try writing the last line this way and tell me if that worked: post.PostedBy.ToString() – Emad Khalil Oct 13 '15 at 13:43