MVC 3 Remote Validation, problem with duplicate check in Edit view
Date : March 29 2020, 07:55 AM
Does that help You should use view models. Those are classes which are specifically designed to meet the requirements of a view. Controller actions should take/pass only view models to views and never your domain models. So you will have two controller actions, one for inserting and one for editing, and two corresponding view models with their respective validation rules.
|
How to check if any item is being edited in the QTreeView or not?
Tag : cpp , By : Jay Crockett
Date : March 29 2020, 07:55 AM
This might help you you can check the view's internal state to see if it is in editing state if (my_treeview->state() != QAbstractItemView::EditingState)
{
/* do some stuff */
}
|
Check current Item Id against remote validation MVC
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You need to use AdditionalFields of RemoteAttribute and use the Id (Primary key) of that table. [Remote("DepartmentNameExists", "Department", "Department Name is already taken.", AdditionalFields = "Id")]
public string Name { get; set; }
public JsonResult DepartmentNameExists(string name, int id = 0)
{
return db.Departments.Any(x => x.Name == name.Trim() && x.Id != id)
? Json(string.Format("{0} already exists.", name),
JsonRequestBehavior.AllowGet)
: Json(true, JsonRequestBehavior.AllowGet);
}
|
How can I check the inserted/edited item in jsgrid isn't duplicate?
Date : March 29 2020, 07:55 AM
Does that help The reason is that validate supports only synchronous validation, and validation from our example requires ajax call, so it's asynchronous. You have two options:
|
Custom ValidationAttribute: How to check for duplicate value, ignoring the object being edited
Date : March 29 2020, 07:55 AM
wish of those help The item which is currently being edited most likely has some kind of property to identify it (look it up in the database). Therefore, you need to get that property so you can exclude that when you are searching the database for duplicate tags. Here is how to do that in your custom validation class. I am making the assumption the identifier is named TrainerId: public class UrlTagValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext context)
{
string tag = value as string;
if(string.IsNullOrWhiteSpace(tag))
return new ValidationResult("URL Tag is required.");
var currentTrainer = validationContext.ObjectInstance
as TrainerModel;
if (currentTrainer == null)
{
// What do you want to do? You cannot always return an error
// because another type could be using this custom validation.
// Or you can return an error. Depends on your requirements and
// and usage.
}
using(var dbContext = new OnBoard101Entities())
{
if(dbContext.TrainerDetails.Any(td => td.Tag == tag && td.TrainerId !=
currentTrainer.TrainerId))
{
return new ValidationResult("This URL Tag is not available. Please enter a different one.");
}
}
return ValidationResult.Success;
}
}
|