Reputation: 1733
This is a code snippet of my my Create.cshtml in my ASP.NET MVC3.. I don't know how to save the DateAdded as DateTimeNow. Im using CRUDE to Create a new Record. I already tried to remove the code below and replace it with DateTimeNow (system time) without displaying the textbox and label from the code below:
<div class="editor-label">
@Html.LabelFor(model => model.DateAdded)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DateAdded)
@Html.ValidationMessageFor(model => model.DateAdded)
</div>
Here's my snippet of Create.cshtml
@model PhoneBook.Models.Contact
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Contact</legend>
<div class="editor-label">
@Html.LabelFor(model => model.DateAdded)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DateAdded)
@Html.ValidationMessageFor(model => model.DateAdded)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Upvotes: 0
Views: 566
Reputation: 150253
You should give the DateTime.Now
value in the controller:
[HttpPost]
public ActionResult Foo(Contact model)
{
model.DateAdded = DateTime.Now;
...
...
}
Or if you want the datetime to be in the page when it is rendered:
[HttpGet]
public ActionResult Foo()
{
Contact model = new Contact{ DateAdded = DateTime.Now};
return View(model);
}
Upvotes: 3