Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:14:15+00:00 2026-06-07T00:14:15+00:00

I need to validate a byte[] in my model as Required but whenever I

  • 0

I need to validate a byte[] in my model as Required but whenever I use Data Annotation [Required] on it, it won’t do anything. Even if i choose a file it outputs error message.

Details:

Model:

Public class MyClass
{
   [Key]
   public int ID {get; set;}

   [Required]
   public string Name {get; set;}

   public byte[] Image {get; set;}

   [Required]
   public byte[] Template {get; set;}
}

View:

<div class="editor-label">
   <%:Html.LabelFor(model => model.Image) %>
</div>
<div class="editor-field">
   <input type="file" id="file1" name="files" />
</div>
<div class="editor-label">
   <%:Html.Label("Template") %>
</div>
<div class="editor-field"> 
   <input type="file" id="file2" name="files"/>
</div>
<p>
   <input type="submit" value="Create" />
</p>

I’ve looked around the posts and notice people use custom validation but they have used HttpPostedFileBase as types of files instead of byte[] like me, for some reason when I try to use the same it errors with a missing ID for it… Even though the model has it’s own ID declared.

EDIT:

Context – OnModelCreating additions for Report

modelBuilder.Entity<Report>().Property(p => p.Image).HasColumnType("image");
modelBuilder.Entity<Report>().Property(p => p.Template).HasColumnType("image");

note that i had to put image as ColumnType because of Byte array truncation to a length of 4000. error.

Controller:

public ActionResult Create(Report report, IEnumerable<HttpPostedFileBase> files)
    {

        if (ModelState.IsValid)
        {
            db.Configuration.ValidateOnSaveEnabled = false;

            if (files.ElementAt(0) != null && files.ElementAt(0).ContentLength > 0)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    files.ElementAt(0).InputStream.CopyTo(ms);
                    report.Image = ms.GetBuffer();
                }
            }

            if (files.ElementAt(1) != null && files.ElementAt(1).ContentLength > 0)
            {
                using (MemoryStream ms1 = new MemoryStream())
                {
                    files.ElementAt(1).InputStream.CopyTo(ms1);
                    report.Template = ms1.GetBuffer();
                }

            }
            db.Reports.Add(report);
            db.SaveChanges();

            //Temporary save method
            var tempID = 10000000 + report.ReportID;
            var fileName = tempID.ToString(); //current by-pass for name
            var path = Path.Combine(Server.MapPath("~/Content/Report/"), fileName);
            files.ElementAt(1).SaveAs(path);

            db.Configuration.ValidateOnSaveEnabled = true;
            return RedirectToAction("Index");
        }

Hopefully you may notice what I’m missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:14:17+00:00Added an answer on June 7, 2026 at 12:14 am

    I’ve altered my Create method, and this is what I’ve come up with. Seems to work fine, but…

    if (files.ElementAt(1) != null && files.ElementAt(1).ContentLength > 0)
    {
        using (MemoryStream ms1 = new MemoryStream())
        {
            files.ElementAt(1).InputStream.CopyTo(ms1);
            report.Template = ms1.GetBuffer();
        }
    
    }
    else // this part of code did the trick, although not sure how good it is in practice
    {
        return View(report);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate a url in variable with jQuery, but can't use validate-plugin.
I need to validate the Advanced search form , but it has data to
I need to validate submitted form data: To check whether fields are empty. Proceed
I need to validate as fast as possible and receive the next xml-data on
I need to validate phone number in PHP. I have made following regex, but
I need to validate serial numbers. For this we use regular expressions in C#,
I need validate multiple fields in the JavaScript Framework, but can't figure out how.
I need to validate incoming data in several controllers of a web app before
I need to validate a XML file against a schema. The XML file is
I need help on how to validate Xml file simply? I googled and found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.