This question is locked. New answers and comments are not allowed.
Hi,
Does anyone ever use IValidatableObject in the data model and PopUp mode for the grid editing at the same time?
Now I've got a bug when the data model get updated and fails the validation in the view.
Normally if the data model fails the validation , everyting in the editing PopUp should keep the same as before and the error message should be displayed in the PopUp.
But now the PopUp turns out to be empty ! Weird!!!
Please note this bug is only for editing PopUp,everything goes fine with the inserting PopUp.I don't know what's the difference between the inserting PopUp and editing PopUp.I think they are similar.
I am wondering if someone has same problem as me since this really drive me crazy.
For example:
I have an entity object Application that inherits IValidatableObject interface
The code in the the controller is like this:
Does anyone ever use IValidatableObject in the data model and PopUp mode for the grid editing at the same time?
Now I've got a bug when the data model get updated and fails the validation in the view.
Normally if the data model fails the validation , everyting in the editing PopUp should keep the same as before and the error message should be displayed in the PopUp.
But now the PopUp turns out to be empty ! Weird!!!
Please note this bug is only for editing PopUp,everything goes fine with the inserting PopUp.I don't know what's the difference between the inserting PopUp and editing PopUp.I think they are similar.
I am wondering if someone has same problem as me since this really drive me crazy.
For example:
I have an entity object Application that inherits IValidatableObject interface
#region Application [MetadataType(typeof(ApplicationMetaData))] // Needed for automated data validation public partial class Application : IValidatableObject { #region IValidatableObject Members public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { using (AppCatalogContainer acc = new AppCatalogContainer()) { //check the application name whether exists in database if (acc.Applications.Count(a => (a.Name == this.Name) && (a.Id != this.Id)) > 0) { yield return new ValidationResult("application name already exists", new[] { "Name" }); } //check the application category whether exists in database if (acc.ApplicationCategories.Count(c => c.Id == this.ApplicationCategoryId) == 0) { yield return new ValidationResult("category doesn't exist,please re-choose a category", new[] { "ApplicationCategoryId" }); } } } #endregion } public class ApplicationMetaData { //[ScaffoldColumn(false)] public Int32 Id; //[Required] [DisplayName("Category"), UIHint("ApplicationCategoryId"), Required] public Int32 ApplicationCategoryId { get; set; } [Required(ErrorMessage = "A name is required")] public String Name { get; set; } [Required(ErrorMessage = "A description is required")] public String Description { get; set; } } #endregion public class ApplicationController : Controller { Repository repo = new Repository(); // // GET: /Application/ [ValidateInput(false)] public ActionResult Index(string id) { try { if (!string.IsNullOrEmpty(id)) ViewBag.Application = repo.FindApplication(int.Parse(id)); } catch { ViewBag.Application = null; } ViewBag.ApplicationCategories = repo.GetApplicationCategories().OrderBy(c => c.Name); return View(repo.GetApplications()); } [HttpPost] public ActionResult Insert() { try { Application newapp = new DataModel.Models.Application(); if (TryUpdateModel(newapp)) { repo.InsertApplication(newapp); TempData["Message"] = "Record inserted"; return RedirectToAction("Index", this.GridRouteValues()); } TempData["Message"] = "An error occured while saving the record"; ViewResult result = View("Index", repo.GetApplications()); result.ViewBag.ApplicationCategories = repo.GetApplicationCategories().OrderBy(c => c.Name); return result; } catch (HttpRequestValidationException ve) { TempData["Message"] = "Some special characters are not allowed!" + ve.Message; return RedirectToAction("Index"); } catch (Exception ex) { TempData["Message"] = "Error:" + ex.Message; InfoLogger.LogException(ex, "ApplicationController::Insert"); return RedirectToAction("Index"); } } [HttpPost] public ActionResult Update(int id) { try { Application updatedapp = repo.FindApplication(id); if (updatedapp == null) return RedirectToAction("Index", this.GridRouteValues()); if (TryUpdateModel(updatedapp)) { repo.UpdateApplication(updatedapp); TempData["Message"] = "Record updated"; return RedirectToAction("Index", this.GridRouteValues()); } TempData["Message"] = "An error occured while saving the record"; ViewResult result = View("Index", repo.GetApplications()); result.ViewBag.ApplicationCategories = repo.GetApplicationCategories().OrderBy(c => c.Name); return result; } catch (HttpRequestValidationException ve) { TempData["Message"] = "Some special characters are not allowed!" + ve.Message; return RedirectToAction("Index"); } catch (Exception ex) { TempData["Message"] = "Error:" + ex.Message; InfoLogger.LogException(ex, "ApplicationController::Update"); return RedirectToAction("Index"); } } [HttpPost] [MobileAuthorize(Roles = "Administrators")] public ActionResult Delete(int id) { try { DataModel.Models.Application g = repo.FindApplication(id); if (g == null) return RedirectToAction("Index", this.GridRouteValues()); repo.DeleteApplication(g); TempData["Message"] = "Record deleted"; return RedirectToAction("Index", this.GridRouteValues()); } catch (Exception ex) { TempData["Message"] = "Error:" + ex.Message; InfoLogger.LogException(ex, "ApplicationController::Delete"); return RedirectToAction("Index"); } } }Thanks