-3

Any walkthrough / sample code for ASP.Net (MVC Model) reflecting the way to record the current user's id in a table, and later on accessing only reflecting the said data?

For example a simple model for a SQL table recording sales being made by more than 1 selling officer at the same time, with fields like:

  1. SalesDate
  2. SalesAmount
  3. SalesLocation
  4. SalesOfficer (Automatically recording the user's id or relevant data)

Thus, the fourth field in the sample model above recording the user's id in the background so as to retrieve the same relevant data, when either logged-in by the same user or the administrator next time.

Hopefully, the question is simple and clear enough.

Looking forward for your expert advice, guidance and references in this regard.

Thanks in advance.

  • Asking for off site resources is explicitly [off topic here I'm afraid](https://stackoverflow.com/help/dont-ask). Id's suggest you have a read though the [help] – Liam May 16 '18 at 09:50
  • What is your question? – random May 16 '18 at 09:51
  • The question is simply, how to have the active Individual User Account be somehow tagged to a specific field of a table automatically? In other words, while only being reflected for use, a currently logged-in user records the SalesDate, SalesAmount and SalesLocation, his own id is being recorded in the fourth field of SalesOfficer automatically, thus allowing his employer (one with administrator rights) deriving the complete list so as to conclude the number of sales made by different workers. – Faraz Ahmed Qureshi May 16 '18 at 09:52
  • https://stackoverflow.com/questions/20925822/asp-net-mvc-5-identity-how-to-get-current-applicationuser/26993974 or https://www.codeproject.com/Questions/1170608/Get-current-user-ID-client-MVC-NET – Maryam Ghafarinia May 16 '18 at 10:27
  • Thanks Mary! Excellent resources for sure. – Faraz Ahmed Qureshi May 17 '18 at 06:42

1 Answers1

0

First of all what you can do is get data of SalesDate, SalesAmount , SalesLocation in a model from a view. Now you need to know the Id of current logged in user.if you are using Default authentication in mvc you can get the currently logged in user by:

   string id=System.Web.HttpContext.Current.User.Identity.GetUserId();

if not use appropriate method to get currently logged in user. After that you can add all the records in your database in your ActionMethod.

   [HttpPost]
   public ActionResult Create(SalesModel model)
   {
        tblSales sales=new tblSales();
        sales.SalesDate=model.SalesDate;
        sales.SalesAmount =model.SalesAmount;
        sales.SalesLocation =model.SalesLocation;
        sales.SalesOfficer =id; //This is the id of currently logged in user
        _db.tblSales.Add(sales);
   }
Yogesh
  • 153
  • 1
  • 12
  • Thanks Yogesh! Your advice in respect of using: **System.Web.HttpContext.Current.User.Identity.GetUserId()** sure did turn out to be a valuable one for sure. – Faraz Ahmed Qureshi May 17 '18 at 06:41