How to implement validation on edit ?

Topics: Writing modules
Aug 12, 2013 at 7:34 PM
Controller

public ActionResult Edit(int id)
    {
        var group = _nGroupService.GetNGroup(id);
        var model = _contentManager.BuildEditor(group);
        return View((object)model);
    }
[HttpPost, ActionName("Edit")]
    public ActionResult EditPOST(int id)
    {
        var customer = _nGroupService.GetNGroup(id);
        var model = _contentManager.UpdateEditor(customer, this);


        if (!ModelState.IsValid)
            return View(model);

        _notifier.Add(NotifyType.Information, T("Your customer has been saved"));
        return RedirectToAction("Edit", new { id });
    }
Driver

//POST
    protected override DriverResult Editor(
        NGroupPart part, IUpdateModel updater, dynamic shapeHelper)
    {

        updater.TryUpdateModel(part, Prefix, null, null);
        return Editor(part, shapeHelper);
    }

View

@{ Layout.Title = "Edit Group"; }

@using(Html.BeginFormAntiForgeryPost())
{
@Display(Model)
}

I am very new to orcard , i have implement a controller to update my record as about code ,
but how can i make any validation before i update my record ? or how can i get current edit view data for validation ?
I need to validate no duplicated name b4 i update the record , can any one help ?
Developer
Aug 12, 2013 at 10:30 PM
If your part has data annotations, they will be executed during the TryUpdateModel call.
You can perform custom validation after that call (or before, whatever you want).

After validation, in your controller, check if the ModelState is valid (as you're already doing), and cancel the current transaction in case there are model validation errors.
The reason is that whenever you update a part, it will be persisted to the database at the end of the request, unless you cancel the transaction.
Aug 13, 2013 at 2:55 AM
Ok . but problem is i dont know how to get the current edit view data to do the validation.
In the edit view , i have name and description fields,
and i try using
[HttpPost, ActionName("Edit")]
        public ActionResult EditPOST(int id)
        {
            var customer = _nGroupService.GetNGroup(id);
            string previousName = customer.Name; 
            var model = _contentManager.UpdateEditor(customer, this);

            if (previousName != model.Name)
            {
                if ( ! _nGroupService.VerifyNGroupUnicity(id,model.Name))
                {
                         
                              ModelState.AddModelError("Name", ("Group with same name already exists"));
                           
                }

            }


            if (!ModelState.IsValid)
            {
                Services.TransactionManager.Cancel();
                return View(model);
            }

            _notifier.Add(NotifyType.Information, T("Your customer has been saved"));
            return RedirectToAction("Edit", new { id });
        }
It show me Operator '!=' cannot be applied to operands of type 'string' and 'Orchard.UI.Zones.ZoneOnDemand' in if (previousName != model.Name)

Can i have an example to do the validation ?

thanks
Aug 13, 2013 at 11:04 AM
Yup using the TryUpdateModel
Validation Completed. Thanks
Code :
 [HttpPost, ActionName("Edit")]
        public ActionResult EditPOST(int id)
        {
           
            var ngroup = _contentManager.Get<NGroupPart>(id, VersionOptions.DraftRequired);
          
            if (TryUpdateModel(ngroup))
            {
                if (!_nGroupService.VerifyNGroupUnicity(id, ngroup.Name))
                {
                    ModelState.AddModelError("Name", ("Duplicated name insert not allowed"));
                }
                
            }
        
            if (!ModelState.IsValid)
            {
                Services.TransactionManager.Cancel();

               

                return View () ;
            }

            _notifier.Add(NotifyType.Information, T("Your customer has been saved"));
            return RedirectToAction("Edit", new { id });
        }