"The SponsorId field is required" and I don't want it to be so.

Topics: Troubleshooting
Jun 3, 2011 at 9:12 AM

Hi all,
I've just started using Orchard and I was reading "Creating 1-N and N-N relations".
Orchard looks really cool btw.
I tried to follow instructions under the chapter "relation between content items".
Actually I got inspiration from it for my project.
I managed to do what I wanted except that the field "sponsor" is always required and I don't understand why (I never specified the attribute [Required]).
How can I create the first customer since in the sponsor list there won't be any sponsor whereas this field is required?
How do I handle the default option "Choose a sponsor" which has an empty value (value = "")?
Thanks for your answers
<fieldset>     
  <legend>Sponsor</legend>     
  <div class="editor-field">
 	@Html.DropDownListFor(model => model.SponsorId, Model.Objects
          .Where(o => o.Id != Model.CustomerId)
          .Select(s => new SelectListItem                 
          {                     
             Selected = s.Id == Model.SponsorId,
             Text = s.Name,
             Value = s.Id.ToString() 
          }),             
          T("Choose a sponsor...").Text)     
  </div>
 </fieldset>

Jun 3, 2011 at 9:20 AM

I tried to specify that empty strings are allowed but still it tells me the field is required when I select "Choose a sponsor...".

 

using System.Collections.Generic;
using Orchard.ObjectType.Models;
using Orchard.Object.Models;
using Orchard.Scene.Models;
using System.ComponentModel.DataAnnotations;

namespace Orchard.Object.ViewModels
{
    public class EditSponsorViewModel
    {
        public int CustormerId { get; set; }
        [Required(AllowEmptyStrings = true)]
        public int SponsorId { get; set; }
        public IEnumerable<CustomerViewModel> Customers{ get; set; }
    }
    
    public class CustomerViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Jun 3, 2011 at 10:35 AM

Well it's an int, not a string, so empty strings are meaningless. I think you just need to make the int nullable:

public int? SponsorId { get; set; }

Jun 3, 2011 at 10:57 AM

Awesome. It works! Thank you.

I only had to make a tiny change in UpdateSponsorForContentItem in CustormerService.cs.

Does it seem correct to you?

 

sponsorPart.Sponsor = _contentManager.Get(model.SponsorId ?? -1);

Jun 3, 2011 at 10:58 AM

If it does. You may want to suggest your solution to complete http://www.orchardproject.net/docs/Creating-1-n-and-n-n-relations.ashx#Building_a_Relation_Between_Content_Items_35

Jun 3, 2011 at 11:01 AM

Oops. The created customer wich has no sponsor doesn't show up in the sponsor list while trying to create a new customer.

Jun 3, 2011 at 11:10 AM

Oops again. I must not be awake... I saved the customer but I forgot to publish it.

Jun 3, 2011 at 11:53 AM
Gwena wrote:

Awesome. It works! Thank you.

I only had to make a tiny change in UpdateSponsorForContentItem in CustormerService.cs.

Does it seem correct to you?

 

 

sponsorPart.Sponsor = _contentManager.Get(model.SponsorId ?? -1);

 

I'd do:

if (model.SponsorId.HasValue) {
    sponsorPart.Sponsor = _contentManager.Get(model.SponsorId.Value);
}

But maybe there's something else I'm missing, that n-n relations article should be correct.