public ActionResult Create()
{
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Title");
var a = new Article();
a.CreateDate = DateTime.Now;
return View(a);
}
I cant see the image. Could you please paste the error message by text?
Cheers,
Danny
This is my error by text:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'model' does not exist in the current context
Source Error:
Line 78: </div>
Line 79: <div class="editor-field">
Line 80: @Html.TextBox("CreateDate", model.CreateDate != null? model.CreateDate : DateTime.Now)
Line 81: @Html.ValidationMessageFor(model => model.CreateDate)
Line 82: </div>
Source File: e:\HanetApp\HanetCMS\Views\Article\Create.cshtml Line: 80
Show Detailed Compiler Output:
Show Complete Compilation Source:
Object reference not set to an instance of an object.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 78: </div>
Line 79: <div class="editor-field">
Line 80: @Html.TextBox("CreateDate", Model.CreateDate != null? Model.CreateDate : DateTime.Now) Line 81: @Html.ValidationMessageFor(model => model.CreateDate)
Line 82: </div>
vnhanet
None
0 Points
7 Posts
How to input datetime default value to EditorFor with DateTime.Now()?
Jan 16, 2013 08:05 AM|LINK
Hi all
I have a small project with"
model:
[Display(Name = "Cteate Date")] public DateTime CreateDate { get; set; }Controller:
// // GET: /Article/Create public ActionResult Create() { ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Title"); return View(new Article()); } // // POST: /Article/Create [HttpPost] public ActionResult Create(Article article) { if (ModelState.IsValid) { db.Articles.Add(article); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Title", article.CategoryID); return View(article); }and view:
<div class="editor-label"> @Html.LabelFor(model => model.CreateDate) </div> <div class="editor-field"> @Html.EditorFor(model => model.CreateDate) @Html.ValidationMessageFor(model => model.CreateDate) </div>How to input datetime default value to EditorFor with DateTime.Now()?
Help me please!
Thank you.
ignatandrei
All-Star
135142 Points
21676 Posts
Moderator
MVP
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 16, 2013 08:10 AM|LINK
In the constructor of
put the DateTime value to DateTIme.Nowgoel.ankit
Contributor
2531 Points
513 Posts
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 16, 2013 11:35 AM|LINK
I always have ViewModels for displaying data in Views and for the scenerios you mentioned, put logic in property defined in ViewModel
public class FunEventViewModel { public DateTime BlogDate { get; private set; } public FunEventViewModel(FunEventModel funEventModel) { this.BlogDate = GetBlogDate(funEventModel); } private DateTime GetBlogDate(FunEventModel funEventModel) { if (funEventModel.ModifiedOn.HasValue) { return funEventModel.ModifiedOn.Value; } else if (funEventModel.CreatedOn.HasValue) { return funEventModel.CreatedOn.Value; } return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); } }Ankit
(Please select 'Mark as Answer' if my response has helped you.)
bruce (sqlwo...
All-Star
36856 Points
5448 Posts
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 16, 2013 03:20 PM|LINK
public ActionResult Create() { ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Title"); var a = new Article(); a.CreateDate = DateTime.Now; return View(a); }DannyMualim
Member
260 Points
77 Posts
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 17, 2013 12:46 AM|LINK
Hi Vnhanet,
The easy answer is
In View, update from
To
@Html.TextBox("CreateDate", model.CreateDate != null? model.CreateDate : DateTime.Now)Hopefully, this answers your question.
Danny
vnhanet
None
0 Points
7 Posts
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 17, 2013 01:33 PM|LINK
I coded:
<div class="editor-label"> @Html.LabelFor(model => model.CreateDate) </div> <div class="editor-field"> @Html.TextBox("CreateDate", model.CreateDate != null? model.CreateDate : DateTime.Now) @Html.ValidationMessageFor(model => model.CreateDate) </div>but error with image below:
http://www.mediafire.com/view/?q0dopn9w28pkd3a
Help me please
thank you
DannyMualim
Member
260 Points
77 Posts
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 18, 2013 10:33 AM|LINK
Hi Vnhanet,
I cant see the image. Could you please paste the error message by text?
Cheers,
Danny
vnhanet
None
0 Points
7 Posts
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 18, 2013 12:56 PM|LINK
This is my error by text:
Server Error in '/' Application. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0103: The name 'model' does not exist in the current context Source Error: Line 78: </div> Line 79: <div class="editor-field"> Line 80: @Html.TextBox("CreateDate", model.CreateDate != null? model.CreateDate : DateTime.Now) Line 81: @Html.ValidationMessageFor(model => model.CreateDate) Line 82: </div> Source File: e:\HanetApp\HanetCMS\Views\Article\Create.cshtml Line: 80 Show Detailed Compiler Output: Show Complete Compilation Source:DannyMualim
Member
260 Points
77 Posts
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 18, 2013 01:01 PM|LINK
Uppssss. sorry about it. Could you change from
@Html.TextBox("CreateDate", model.CreateDate != null? model.CreateDate : DateTime.Now)To
@Html.TextBox("CreateDate", Model.CreateDate != null? Model.CreateDate : DateTime.Now)I forgot that the model should be use capital M, so it should be Model.
Cheers,
Danny
vnhanet
None
0 Points
7 Posts
Re: How to input datetime default value to EditorFor with DateTime.Now()?
Jan 18, 2013 01:54 PM|LINK
After that, my view is:
<div class="editor-label"> @Html.LabelFor(model => model.CreateDate) </div> <div class="editor-field"> @Html.TextBox("CreateDate", Model.CreateDate != null? Model.CreateDate : DateTime.Now) @Html.ValidationMessageFor(model => model.CreateDate) </div>but it made an error:
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 78: </div> Line 79: <div class="editor-field"> Line 80: @Html.TextBox("CreateDate", Model.CreateDate != null? Model.CreateDate : DateTime.Now) Line 81: @Html.ValidationMessageFor(model => model.CreateDate) Line 82: </div>Source File: e:\HanetApp\HanetCMS\Views\Article\Create.cshtml Line: 80
Stack Trace:
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929