[RESOLVED]Model state still returns false after I make changes as per the required validations.

My Model state still returns false after I make changes as per the  required validations.

 [HttpPost]
        public ActionResult InsertProject(Project project, HttpPostedFileBase ImageFile = null)
        {

            project.RegistrationId = 1;//Testing
            if (ModelState.IsValid)
            {
                if (ImageFile != null)
                {
                    project.ImageType = ImageFile.ContentType;
                    project.Image = new byte[ImageFile.ContentLength];
                    ImageFile.InputStream.Read(project.Image, 0, ImageFile.ContentLength);
                }
                repository.InsertProject(project);
                return RedirectToAction("List");
            }
            
            return View();
        }

This is part of my view

@using (Html.BeginForm("InsertProject", "Project", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    if (ViewData.ModelState.Keys.Any(k => ViewData.ModelState[k].Errors.Count() > 0))
    { 
   <div class="alert alert-warning">@Html.ValidationSummary()</div>
    }

So when user inputs wrong data, validation summary shows up. After making the required changes when user submits the form, the model state is still invalid. I’m pretty new to mvc. Could you please guide me.

Thanks,

Rose

Show also the Project class code. Which validators you have on it? 

 
@OP: I didn’t answer the question, I only asked to show code around Project class. Please unmark my post as an answer, If your problem is solved then you can update the post.

Yes..Show also the Project class code. Which validators you have on it? this will help us to understand the actual reason for Failing your required field. 

Leave a Reply