Stopping a field from being required

Topics: Writing modules
Aug 9, 2011 at 2:14 PM

I have a part that I created and attached to a page.  The part basically allows you to select a user from a dropdown list and send them an email notification...the problem is that I also want it to not be required, so if you keep the dropdownlist at "Select..." it won't do anything.  Basically it'll only respond if you choose a user, otherwise it'll ignore the part.  For some reason, the part keeps telling me that the AssignedUserId is required, even though I haven't decorated the model with [Required].  This part saves a record to the database as well to log that an email was sent.  I originall just put all the virtual properties on the PartRecord itself, but because the data relates to ContentItem and can contain multiple records tied to that ContentItem, I was having some issues with that scenario, so instead I moved the virtual properties to just a Record instead of a PartRecord.  Here's the code:


public class PublishFlowRecord
        public virtual int Id { get; set; }
        public virtual ContentItemRecord ContentItemRecord { get; set; }
        public virtual string Note { get; set; }
        public virtual bool ShowNotification { get; set; }
        public virtual UserPartRecord AssignedUser { get; set; }
        public virtual UserPartRecord RequestUser { get; set; }
        public virtual DateTime? CreateDate { get; set; }
        public virtual bool IsActive { get; set; }

 public class PublishFlowPartRecord : ContentPartRecord
        // don't need to map this, nothing is getting stored for this

    public class PublishFlowPart : ContentPart<PublishFlowPartRecord>
       // These properties are used when the form posts back, but are not actually stored in the database
        public bool ShowForm { getset; }
        public int AssignedUserId { getset; }
        public string Note { getset; }

        public SelectList UserList { getset; }

        public PublishFlowRecord PublishFlowRecord { getset; }
        public IEnumerable<PublishFlowRecord> PublishFlowHistory { getset; }


protected override DriverResult Editor(PublishFlowPart part, dynamic shapeHelper) {

            var model = LoadModel(part);

            return ContentShape("Parts_PublishFlow_Edit",
                                () => shapeHelper.EditorTemplate(
                                    TemplateName: "Parts/PublishFlow",
                                    Model: model,
                                    Prefix: Prefix));

        protected override DriverResult Editor(PublishFlowPart part, IUpdateModel updater, dynamic shapeHelper)
            updater.TryUpdateModel(part, Prefix, null, null);

            // If the user selected a reviewer to assign to
            if (part.AssignedUserId > 0)
                var user = _orchardService.WorkContext.CurrentUser;
                var contentItem = _contentItemRepository.Get(part.Id);
                var requestUser = _userRepository.Get(part.AssignedUserId);

                // Get current active publishflow record and deactivate it

                // Create a new publishflow record and activate that one
                _publishFlowRepository.Create(new PublishFlowRecord
                    Note = part.Note,
                    AssignedUser = requestUser,
                    RequestUser = _userRepository.Get(user.Id),
                    ShowNotification = true,
                    CreateDate = DateTime.Now,
                    IsActive = true,
                    ContentItemRecord = contentItem

                // Send email notification to new reviewer
                if (requestUser.Email != string.Empty) SendEmail(requestUser.Email, part.Note, contentItem.Id);

            var model = LoadModel(part);

            return ContentShape("Parts_PublishFlow_Edit",
                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));


            Assign to: @Html.DropDownList("AssignedUserId", Model.UserList, "Select...", new { id = "UsersList" })        
            <div id='ReviewersContainer' style='display:none;'>






Aug 9, 2011 at 2:36 PM

Of course, after I posted this, I found the fix...I needed to make the AssignedUserId nullable and check for null instead of > 0, like this:

if (part.AssignedUserId != null) {
                var assignedUserId = (int) part.AssignedUserId;

I'm fully aware that what I'm doing overall for this whole task is probably violating some Orchard standards, but this is the only way I could think of to pull off what I'm trying to do.