Reputation: 5918
I have a create action that doesn't send the CreatedOn and CreatedBy back to the HttpPost create action.
These are not user definable properties, and ideally i don't want them displayed on the form at all. So how do i get these properties into the model, without having them on the form itself? Should they be hidden fields on the form?
The Controller
public virtual ActionResult Create()
{
var meeting = new Meeting
{
CreatedOn = DateTime.Now,
CreatedBy = User.Identity.Name,
StartDate = DateTime.Now.AddMinutes(5),
EndDate = DateTime.Now.AddHours(3)
};
ViewBag.Title = "Create Meeting";
return View(meeting);
}
[HttpPost]
public virtual ActionResult Create(Meeting meeting)
{
if (ModelState.IsValid)
{
_meetingRepository.InsertOrUpdate(meeting);
_meetingRepository.Save();
return RedirectToAction(MVC.Meetings.Details(meeting.MeetingId));
} else {
return View();
}
}
Upvotes: 1
Views: 340
Reputation: 1039418
Should they be hidden fields on the form?
Yes, that's definitely one good way of passing them. Note that this is not secure because the user can fake a POST request and modify them but that could be OK in your scenario.
So if you need security another way is to re-fetch them from the data store as the user cannot modify them in this form so they haven't changed.
Upvotes: 2