MVC and Handling data [Resolved]

Posted by Rajeshmoorthy under ASP.NET MVC on 11/25/2015 | Points: 10 | Views : 644 | Status : [Member] | Replies : 4
I have a column in my model which requires the data to be passed while saving / modifying data while save and not captured in the screen. for eg, Created date with datetime.now and the created by with the logged in user id.

How do I do that?? I am using mvc5 with visual studio 2013 and Annotations.

Thanks
Rajesh Moorthy



Responses

Posted by: Rajnilari2015 on: 11/25/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Suppose you have a model as

public class MyModel{


public DateTime CreatedDate {get;set;}
public string CreatedBy{get;set;}
}

then from your controller you can do e.g.

 

public class MyTestController : Controller{

MyModel mc = new MyModel();

mc.CreatedDate = DateTime.Now;
mc.CreatedBy = RequestContext.Principal.Identity.GetUserId();
}


Or if you want to pass from View to controller, you can do so by
@User.Identity.Name
after capturing in a hidden field.Likewise for CreatedDate.

And capture those values in controller action method.

hope this helps

--
Thanks & Regards,
RNA Team

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajeshmoorthy on: 11/26/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks for the prompt reply. Let me try and revert

Thanks

Rajesh

Thanks
Rajesh Moorthy

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajeshmoorthy on: 12/4/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks to the RNA TEam. That worked!!

Thanks
Rajesh Moorthy

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/20/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Rajeshmoorthy , glad it helped

--
Thanks & Regards,
RNA Team

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response