Followers

Friday 29 January 2016

Set value for joined table in MVC Razor

Source: stackoverflow.com --- Thursday, January 28, 2016
I'm trying to create a razor page for a Windows auth MVC app that tracks who created the record. The basic idea looks like this: public class User { public int Id {get;set;} public string NetworkId {get;set;} } public class Event { public int Id {get;set;} public int UserId {get;set;} public DateTime Created {get;set;} public virtual User User {get;set;} } On the Razor page, I'm trying to create a HiddenFor for to set the user ID , but I can't get it to post correctly. Setting the Created field works correctly ( @Html.HiddenFor(model => model.Created, new { Value = DateTime.Now }) ), but the UserId won't set. This doesn't work: @Html.HiddenFor(model => model.User.NetworkId, new { Value = User.Identity.Name }) This throws a null reference exception: @{ Model.User.NetworkId = User.Identity.Name; } @Html.HiddenFor(model => model.User.NetworkId) There has to be an easier way to do this, other than looking for the NetworkId on the table and pulling back the actual primary key. Any ideas? ...



from Windows http://ift.tt/1Ut0SI3

No comments:

Post a Comment