This is a migrated thread and some comments may be shown as answers.

[Solved] Server side validation problem

0 Answers 129 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Steve
Top achievements
Rank 1
Steve asked on 08 Sep 2010, 02:05 AM
Hi,

    I'm using version  2010.2.825 with a grid server bound.  When I click Edit, I add model error if field DescriptionAutre and course type is Autre.  It works fine, but if I have an error, the url is changed to Update (instead of Index) and application stop working when I cancel editing.

Here is the code of Update method.

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update(Guid id, FormCollection collection)
        {
            //Find a customer whose CustomerID is equal to the id action parameter
            var competence = _container.GetCompetenceById(id);
            competence.fk_CoursId_TypeCours = int.Parse(collection["TypeCours"]);
  
            if (competence == null)
            {
                //A customer with the specified id does not exist - redisplay the grid
  
                return RedirectToAction("Index", new { membreId = Guid.Empty });
            }
  
            if (string.IsNullOrWhiteSpace(collection["DescriptionAutre"]) && competence.fk_CoursId_TypeCours == (int)Models.TypeCours.COURS_ID.Autre)
            {
                this.ModelState.AddModelError("DescriptionAutre", "La description est obligatoire lorsque vous sélectionnez le type de cours 'Autre'");
            }
  
            //Perform model binding (fill the customer properties and validate it).
            if (TryUpdateModel(competence, null, null, new string[] { "TypeCours" }))
            {
                //The model is valid - update the customer and redisplay the grid.
                _container.SaveChanges();
  
                return RedirectToAction("Index", new { membreId = competence.fk_MembreId_Membres });
            }
  
            //The model is invalid - render the current view to show any validation errors
            return Index(competence.fk_MembreId_Membres);
        }

Here is the code for Index method:
public ActionResult Index(Guid membreId)
{
    var viewData = new ViewData.MemberWithCompetences();
    viewData.Membre = _container.GetMemberById(membreId);
    viewData.Competences = _container.GetCompetenceForMember(membreId);
    PopulateTypeCours();
    return View("Index", viewData);
}

If you have a shorter way to make those little validations client-side, go ahead, I can change the way to do those validations....

Thank you
Steve
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Share this question
or