Haidar
Haidar

Reputation: 123

How to ignore data annotation validation in asp.net MVC

Is there any way to ignore data annotation validation in asp.net validation?

Look at the following case:

I have this metadata class for a message entity

public class MessageMetaData
{
    [Required(ErrorMessage="This field is required")]        
    public int[] To { get; set; }

    [Required(ErrorMessage = "This field is required")]        
    public string Title { get; set; }

    [Required(ErrorMessage = "This field is required")]        
    public string Body { get; set; }
}

and this is my message class:

public partial class message
{
    public message()
    {
        this.FilesRelatedToTheMessage = new HashSet<file>();
        this.Receivers = new HashSet<MessageReceiversConnector>();
    }

    public int id { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public Nullable<int> C_From { get; set; }
    public Nullable<int> AreaId { get; set; }
    public Nullable<int> Project { get; set; }
    public string Status { get; set; }
    public System.DateTime DateOfCreation { get; set; }

    public virtual ICollection<file> FilesRelatedToTheMessage { get; set; }
    public virtual EventsArea EventsArea { get; set; }
    public virtual project ProjectTheMessageRelatedTo { get; set; }
    public virtual ICollection<MessageReceiversConnector> Receivers { get; set; }
    public virtual user Sender { get; set; }
}

[MetadataType(typeof(MessageMetaData))]
public partial class message
{
    public int[] To { get; set; }
    public int[] Areas { get; set; }
    private MessageDTO mdto = new MessageDTO();
    public MessageDTO MDTO { get { return this.mdto; } set { this.mdto = value; } }
}

When I want to store a message as a draft message, one of the properties might not be sent. Is there any way to handle this issue?

Edit:

this is my DraftMessage action

    public string DraftMessage(message draftMessage, string status)
    {
        if (status == "new")
        {
            draftMessage.Status = "DR";
            draftMessage.DateOfCreation = DateTime.UtcNow;
            draftMessage.C_From = CurrentUser.Id;
            context.messages.Add(draftMessage);
            foreach (int receiverId in draftMessage.To)
            {
                if (context.users.Any(user => user.user_id == receiverId && (user.DepartmentOfTheUser.Company == CurrentUser.Company || user.DepartmentOfTheUser.CompanyOfTheDepartment.CompanyCustomers.Any(cus => cus.customer_of == CurrentUser.Company) || user.DepartmentOfTheUser.CompanyOfTheDepartment.CompanySuppliers.Any(supp => supp.supplier_of == CurrentUser.Company))))
                {
                    context.MessageReceiversConnectors.Add(new MessageReceiversConnector() { MessageId = draftMessage.id, ReceiverId = receiverId, MessageStatus = "DR" });
                }
            }
        }

        context.SaveChanges();

        return "draft";
    }

here it throws message on context.SaveChanges(); because of validation, the fields in the database are all nullable but it throws exception due to metadata class.

Upvotes: 0

Views: 1276

Answers (1)

Luke
Luke

Reputation: 126

Maybe it is worth to make two ViewModels. One for Draft message and another for message you want to send.

Upvotes: 1

Related Questions