DateTimeField not populating in edit mode

Topics: Administration, General
Jan 16, 2014 at 6:17 AM
Edited Jan 16, 2014 at 11:20 AM
I am trying to integrate DateTimeField in module, it works great although it does not populate in edit mode.

Create View

@model Orchard.Club.ViewModels.CourseCreateViewModel

@{
    Style.Include("datetime.css");
    
    Script.Require("jQuery").AtFoot();
    Script.Require("jQueryUI_Core").AtFoot();
    Script.Require("jQueryUI_Widget").AtFoot();
    Style.Require("jQueryUI_Orchard").AtFoot();
    Script.Include("hint.js").AtFoot();
}

<fieldset>     

     <label for="@Html.FieldIdFor(m => Model.StartDate.Date)"</label>        
     <label class="forpicker" for="@Html.FieldIdFor(m => Model.StartDate.Date)">@T("Date")</label>
     <span class="date">@Html.EditorFor(m => m.StartDate.Date)</span>

     <label for="@Html.FieldIdFor(m => Model.EndDate.Date)"</label>        
     <label class="forpicker" for="@Html.FieldIdFor(m =>m.EndDate.Date)">@T("Date")</label>
     <span class="date">@Html.EditorFor(m => m.EndDate.Date)</span>
    
</fieldset>


@using(Script.Foot()) {

    @* generates the localization script *@
     @Display.DatePickerLocalization() 
     
<script type="text/javascript">    
    $(function () {
        $("#@Html.FieldIdFor(m => Model.StartDate.Date)").datepicker({ showAnim: "" });
        $("#@Html.FieldIdFor(m => Model.EndDate.Date)").datepicker({ showAnim: "" });
    })
</script>
}

Edit View

@model Orchard.Club.ViewModels.CourseEditViewModel

@{
    Style.Include("datetime.css");
    
    Script.Require("jQuery").AtFoot();
    Script.Require("jQueryUI_Core").AtFoot();
    Script.Require("jQueryUI_Widget").AtFoot();
    Style.Require("jQueryUI_Orchard").AtFoot();
    Script.Include("hint.js").AtFoot();
}

<fieldset>     

     <label for="@Html.FieldIdFor(m => Model.StartDate.Date)"</label>        
     <label class="forpicker" for="@Html.FieldIdFor(m => Model.StartDate.Date)">@T("Date")</label>
     <span class="date">@Html.EditorFor(m => m.StartDate.Date)</span>

     <label for="@Html.FieldIdFor(m => Model.EndDate.Date)"</label>        
     <label class="forpicker" for="@Html.FieldIdFor(m =>m.EndDate.Date)">@T("Date")</label>
     <span class="date">@Html.EditorFor(m => m.EndDate.Date)</span>
    
</fieldset>


@using(Script.Foot()) {

    @* generates the localization script *@
     @Display.DatePickerLocalization() 
     
<script type="text/javascript">    
    $(function () {
        $("#@Html.FieldIdFor(m => Model.StartDate.Date)").datepicker({ showAnim: "" });
        $("#@Html.FieldIdFor(m => Model.EndDate.Date)").datepicker({ showAnim: "" });
    })
</script>
}

Create ViewModel

public class CourseCreateViewModel  {

        public DateTimeFieldViewModel StartDate
        {
            get;
            set;
        }
        
        public DateTimeFieldViewModel EndDate
        {
            get;
            set;
        }
    }

EditViewModel

public class CourseEditViewModel  {

        public DateTimeFieldViewModel StartDate
        {
            get;
            set;
        }
        
        public DateTimeFieldViewModel EndDate
        {
            get;
            set;
        }
    }

Controller Snippet

 public ActionResult Create()
        {

            DateTimeField field = new DateTimeField();
            var value = DateTime.Now;

            DateTimeFieldViewModel startDateVM = new DateTimeFieldViewModel() { Name = "Start Date", Date = string.Empty, Time = string.Empty, ShowDate = false, ShowTime = false, Hint = "Enter Start Date", Required = false };
            DateTimeFieldViewModel endDateVM = new DateTimeFieldViewModel() { Name = "End Date", Date = string.Empty, Time = string.Empty, ShowDate = false, ShowTime = false, Hint = "Enter End Date", Required = false };

            CourseCreateViewModel _model = new CourseCreateViewModel();
            _model.StartDate = startDateVM;
            _model.EndDate = endDateVM;

            var user = _contentManager.New("Course");
            var editor = Shape.EditorTemplate(TemplateName: "Parts/Course.Create", Model: _model, Prefix: null);
            editor.Metadata.Position = "2";
            dynamic model = _contentManager.BuildEditor(user);
            model.Content.Add(editor);

            return View((object)model);
        }

        [HttpPost, ActionName("Create")]
       public ActionResult CreatePOST(CourseCreateViewModel createModel)
        {
            var contentItem = _contentManager.New<CoursePart>("Course");

            contentItem.Name = createModel.Name;
            contentItem.Description = createModel.Description;
            contentItem.Location = createModel.Location;
            contentItem.Category = createModel.Category;
            contentItem.StartDate = Convert.ToDateTime(createModel.StartDate.Date);
            contentItem.EndDate = Convert.ToDateTime(createModel.EndDate.Date);
            contentItem.Fees = createModel.Fees;         
            

            _contentManager.Create(contentItem);
                       
            _orchardService.Notifier.Information(new LocalizedString("Course has been created."));                                        

            return RedirectToAction("Index");
        }

       public ActionResult Edit(int id)
        {
            var course = _orchardService.ContentManager.Get<CoursePart>(id);

            DateTimeFieldViewModel startDateVM = new DateTimeFieldViewModel() { Name = "Start Date", Date = course.StartDate.ToShortDateString() , Time = string.Empty, ShowDate = false, ShowTime = false, Hint = "Enter Start Date", Required = false };
            DateTimeFieldViewModel endDateVM = new DateTimeFieldViewModel() { Name = "End Date", Date = course.EndDate.ToShortDateString(), Time = string.Empty, ShowDate = false, ShowTime = false, Hint = "Enter End Date", Required = false };

            CourseEditViewModel _model = new CourseEditViewModel();
            _model.StartDate = startDateVM;
            _model.EndDate = endDateVM;

            var editor = Shape.EditorTemplate(TemplateName: "Parts/Course.Edit", Model: new CourseEditViewModel { Course = course }, Prefix: null);
            editor.Metadata.Position = "2";
            dynamic model = _orchardService.ContentManager.BuildEditor(course);
            model.Content.Add(editor);
            
            return View((object)model);
        }

        [HttpPost, ActionName("Edit")]
      public ActionResult EditPOST(int id)
        {            
            var course = _orchardService.ContentManager.Get<CoursePart>(id);
            dynamic model = _orchardService.ContentManager.UpdateEditor(course, this);

            _orchardService.ContentManager.Publish(course.ContentItem);
            _orchardService.Notifier.Information(new LocalizedString("Course information updated"));

            return RedirectToAction("Index");
        }