Selected Date Not Saving in Orchard Module

Topics: Administration, General
Jan 10, 2014 at 11:03 AM
Edited Jan 13, 2014 at 10:06 AM
Orchard is not saving the StartEnd and EndDate fields what i selected from View. Even i assign the selected date values in Create (Post) method
as
            _course.StartDate = createModel.StartDate.Value;
            _course.EndDate = createModel.EndDate.Value;

Controller

 public ActionResult Create()
        {                     

            var user = _contentManager.New("Course");
            var editor = Shape.EditorTemplate(TemplateName: "Parts/Course.Create", Model: new CourseCreateViewModel(), 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 = createModel.StartDate;
            contentItem.EndDate = createModel.EndDate;
            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);
            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");
        }

CourseEditViewModel

  public class CourseEditViewModel  
    {     
        [Required]
        public string Name
        {
            get { return Course.As<CoursePart>().Record.Name; }
            set { Course.As<CoursePart>().Record.Name = value; }
        }
        
        public string Description
        {
            get { return Course.As<CoursePart>().Record.Description; }
            set { Course.As<CoursePart>().Record.Description = value; }
        }

        public string Location
        {
            get { return Course.As<CoursePart>().Record.Location; }
            set { Course.As<CoursePart>().Record.Location = value; }
        }

        public string Category
        {
            get { return Course.As<CoursePart>().Record.Category; }
            set { Course.As<CoursePart>().Record.Category = value; }
        }

        public double Fees
        {
            get { return Course.As<CoursePart>().Record.Fees; }
            set { Course.As<CoursePart>().Record.Fees = value; }
        }

        [DataType(DataType.Date)]
        public DateTime StartDate
        {
            get { return Course.As<CoursePart>().Record.StartDate.Date; }
            set { Course.As<CoursePart>().Record.StartDate = value; }
        }

        [DataType(DataType.Date)]
        public DateTime EndDate
        {
            get { return Course.As<CoursePart>().Record.EndDate.Date; }
            set { Course.As<CoursePart>().Record.EndDate = value; }
        }

        public IContent Course { get; set; }
    }

CoursePart

    public class CoursePart : ContentPart<CoursePartRecord>
    {
        public string Name
        {
            get { return Record.Name; }
            set { Record.Name = value; }
        }

        public string Description
        {
            get { return Record.Description; }
            set { Record.Description = value; }
        }

        public string Location
        {
            get { return Record.Location; }
            set { Record.Location = value; }
        }

        public string Category
        {
            get { return Record.Category; }
            set { Record.Category = value; }
        }

        
        public DateTime StartDate
        {
            get { return Record.StartDate; }
            set { Record.StartDate = value; }
        }

        
        public DateTime EndDate
        {
            get { return Record.EndDate; }
            set { Record.EndDate = value; }
        }

        public double Fees
        {
            get { return Record.Fees; }
            set { Record.Fees = value; }
        }
    }

    public class CoursePartRecord : ContentPartRecord
    {
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual string Location { get; set; }
        public virtual string Category { get; set; }
        public virtual DateTime StartDate { get; set; }
        public virtual DateTime EndDate { get; set; }
        public virtual double Fees { get; set; }
    }

CoursePartDriver

    public class CourserPartDriver : ContentPartDriver<CoursePart> {

        protected override DriverResult Display(
            CoursePart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_Course",
                () => shapeHelper.Parts_Course(
                    Name: part.Name,
                    Description: part.Description));
        }
    }
Developer
Jan 10, 2014 at 12:03 PM
In your Create (POST) controller action, it looks like you are first assigning the view model properties to the _course part. But a little later, you are invoking UpdateEditor on the content item which has the CoursePart attached. Now it's hard to say what happens there because you didn't provide the code for CoursePartDriver's Editor (POST) method, but perhaps you are updating the date time fields there? Or perhaps from a content handler as well?

Out of curiosity, what is the reason you are updating your CoursePart from the controller action, instead of doing it from the CoursePart driver?
Jan 10, 2014 at 12:35 PM
I have updated the original post, now i have removed the line which invoke UpdateEditor. Nothing special in Driver because i am using Static View Model approach. Yes i am assigning dates in Handler but if i remove the code
     protected void AssignDates(InitializingContentContext context, CoursePart part)
        {
            part.StartDate = DateTime.Now;
            part.EndDate = DateTime.Now;
        } 
i dont get any dates saved in database. However it should be the dates i selected in View
Jan 13, 2014 at 10:09 AM
Edited Jan 13, 2014 at 10:10 AM
I am able to successfully save dates to database and can see proper date values in List. However i am not able to do proper editing. When i go to edit mode i make changes to any field e.g. Name, after saving it does not reflect back. Please see the latest code in my original post.
Jan 20, 2014 at 6:33 PM
Is there any errors in your log file? If so, please provide the error?