AAaaAA
AAaaAA

Reputation: 85

ASP.NET MVC changing post method to save userId

I'm new to ASP.Net MVC, learning it on my own. I am trying to create a web-app to allow users to send requests online, simply I've to Tables in my Database, using IdentityDbContext to generate userTable, and I got another Table 'Fatwa", the user sends a request and saves them in Fatwa Table with UserID, to reference which user sends the request.

my problem is I got the POST request using scaffolding, it works fine, but I don't know how to Attach the userID with the Post request,

I did make it a ForeignKey in the database but don't know how to edit my post request to allow it to save the userID with the request.

please help me

thank you

this is the POST request

   // POST: api/Fatwas
        [HttpPost]
        public async Task<IActionResult> PostFatwa([FromBody] Fatwa fatwa)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _context.Fatwa.Add(fatwa);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetFatwa", new { id = fatwa.FatwaID }, fatwa);
        }

Upvotes: 0

Views: 47

Answers (1)

moath naji
moath naji

Reputation: 661

if i understand well you need the current user id, create object of user inside the class of Fatwa to make the relation between the user and fatwa then you can get the user from the current context HttpContext context inside the context there are context.User?.Identity.Name and much more

Upvotes: 2

Related Questions