How does NerdDinner's AddModelErrors work? - asp.net-mvc

I'm going through the NerDinner free tutorial
http://nerddinnerbook.s3.amazonaws.com/Intro.htm
I got to somewhere in Step 5 where it says to make the code cleaner we can create an extension method. I look at the completed code and it has this to use the extension method:
catch
{
ModelState.AddModelErrors(dinner.GetRuleViolations());
return View(new DinnerFormViewModel(dinner));
}
And then this as the extension method's definition.
namespace NerdDinner.Helpers {
public static class ModelStateHelpers {
public static void AddModelErrors(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors) {
foreach (RuleViolation issue in errors) {
modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
}
}
}
I try to follow what the tutorial says combined with what the code contains but receive the expected error that there is no AddModelErrors method that accepts only 1 argument.
I'm obviously missing something very important here. What is it?

You need to include the helpers reference;
using NerdDinner.Helpers;
and
using NerdDinner.Models;
Then check for valid and add the errors;
if (!dinner.IsValid)
{
ModelState.AddModelErrors(dinner.GetRuleViolations());
return View(dinner);
}
You must also have a partial class for your dinner;
public partial class Dinner
{
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty( SomeField ))
yield return new RuleViolation("Field value text is required", "SomeField");
}
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
}
Don't forget the RuleViolation class;
public class RuleViolation
{
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation(string errorMessage)
{
ErrorMessage = errorMessage;
}
public RuleViolation(string errorMessage, string propertyName)
{
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}

If you are receiving the same error message as this poster:
"'System.Web.Mvc.ModelStateDictionary' does not contain a definition for 'AddModelErrors' and no extension method 'AddModelErrors' accepting a first argument of type 'System.Web.Mvc.ModelStateDictionary' could be found (are you missing a using directive or an assembly reference?)"
You may be having this problem:
http://p2p.wrox.com/book-professional-asp-net-mvc-1-0-isbn-978-0-470-38461-9/74321-addmodalerrors-allcountries-page-87-view-data-dictionary.html#post248356

Related

With ASP.NET MVC, how to display errors when outside controller?

I'm trying to easily display errors in my View from anywhere in my code using :
#Html.ValidationSummary("", new { #class = "text-danger" })
Before MVC, I used :
ValidationError.Display("My error message");
And my ValidationError class looks like this:
public class ValidationError : IValidator
{
private ValidationError(string message)
{
ErrorMessage = message;
IsValid = false;
}
public string ErrorMessage { get; set; }
public bool IsValid { get; set; }
public void Validate()
{
// no action required
}
public static void Display(string message)
{
// here is the only part I would like to change ideally
var currentPage = HttpContext.Current.Handler as Page;
currentPage.Validators.Add(new ValidationError(message));
}
}
Now with MVC, to add errors, I can't use currentPage.Validators.
I need to use ModelState but my problem is that I can't access ModelState when I'm not in the Controller. I tried accessing the controller or the ModelState via HttpContext but I've not found a way to do it. Any idea ?
ModelState.AddModelError("", "My error message");
1. You can access it through ViewContext.ViewData.ModelState. Then use
#if (!ViewContext.ViewData.ModelState.IsValid)
{
<div>There are some errors</div>
}
OR
ViewData.ModelState.IsValidField("NameOfInput")
get a list of inputs:
var errors = ViewData.ModelState.Where(n => n.Value.Errors.Count > 0).ToList();
2. You can pass your model state around like this:
public class MyClass{
public static void errorMessage(ModelStateDictionary ModelState) {
if (something) ModelState.AddModelError("", "Error Message");
}
}
Use in controller:
MyClass.errorMessage(ModelState);
Use in view:
MyClass.errorMessage(ViewContext.ViewData.ModelState.IsValid);
3. ModelState via ActionFilter
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.Controller.ViewData.ModelState.IsValid)
{
//Do Something
}
}
}
You can get more help from this and this links.

Error with dependency injection and generic base class

I am learning repository and unit of work patterns using generics and dependency injection. I have a persistent error that I have been churning on and I am clearly missing something fundamental.
I am getting the following error which whilst I understand that it is looking for a database table column called 'Id'
Because my Entity class inherits from BaseEntity I suspect this is the problem but I don't understand why and what I am best to do to fix it.
public class BaseEntity<T>
{
public T Id { get; set; }
}
Error returned
An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' >occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: An error occurred while executing the command >definition. See the inner exception for details.
Inner exception
{"Invalid column name 'Id'."}
To add some context I am using EF6 MVC5 and AutoFac for the Dependancy injection.
Entity class
(A column 'id' doesn't exist in the database - the key in the database is 'EmrgencyAttendanceId') and the Entity class also sets the key as 'EmrgencyAttendanceId' like so.
[Table("reporting.EDISRecords")]
public class EDISRecord : BaseEntity<int>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EmergencyAttendanceId { get; set; }
[StringLength(50)]
public string Hospital { get; set; }
[StringLength(20)]
public string URNumber { get; set; }
Controller action
The controller calls the GetRecord method of the EDISRecordService passing a Lambda expression. I use DbFunctions.TruncateTime because the database stores as date time and I only want to search on the date.
public ActionResult Search(string Date, string CaseNumber)
{
if (!string.IsNullOrEmpty(Date) || !string.IsNullOrEmpty(CaseNumber))
{
DateTime dt = DateTime.Parse(Date);
var EmergencyAttendance = _edisRecordService.GetRecord(m => (DbFunctions.TruncateTime(m.ArrivalDateTime) == dt) && (m.RTAIdentifier == CaseNumber));
//for initialising view model
SeperationSummaryViewModel model = new SeperationSummaryViewModel();
//assign values for view model
if (EmergencyAttendance != null)
{
if (EmergencyAttendance.DepartureDestination != null)
{
if (EmergencyAttendance.DepartureDestination.Substring(0, 1) == ".")
{
model.DepartureDestination = EmergencyAttendance.DepartureDestination.Substring(1);
}
else
{
model.DepartureDestination = EmergencyAttendance.DepartureDestination;
}
}
else
{
model.DepartureDestination = "Not recorded by Emergency Department";
}
if (EmergencyAttendance.InpatientAdmissionDiagnosis != null)
{
model.InpatientAdmissionDiagnosis = EmergencyAttendance.InpatientAdmissionDiagnosis;
}
else
{
model.InpatientAdmissionDiagnosis = "Not recorded by Emergency Department";
}
}
//send view model into UI (View)
return PartialView("_SeperationInformationPartialView", model);
}
else
{
if (string.IsNullOrEmpty(Date) || string.IsNullOrEmpty(CaseNumber))
{
return PartialView("Blank");
}
}
return PartialView("Error");
}
Service Class
The service class instantiates a unit of work and calls the repository Get method passing through the Lambda expression.
public class EDISRecordService : IEDISRecordService
{
private readonly IUnitOfWork<DataWarehouseDataManager> _unitOfWork;
public EDISRecordService(IUnitOfWork<DataWarehouseDataManager> unitOfWork)
{
_unitOfWork = unitOfWork;
}
public EDISRecord GetRecord(Expression<Func<EDISRecord, bool>> #where)
{
return _unitOfWork.EDISRecordRepository.Get(#where);
}
}
RepositoryBase class
The repository class inherits from the repository base class which impliments the Get method passing the lambda to 'Expression> #where'
public class RepositoryBase<TEntity> : Disposable, IRepository<TEntity>
where TEntity : class
{
private readonly DbContext _dataContext;
private IDbSet<TEntity> Dbset
{
get { return _dataContext.Set<TEntity>(); }
}
public RepositoryBase(DbContext dbContext)
{
_dataContext = dbContext;
}
public TEntity Get(Expression<Func<TEntity, bool>> #where)
{
return Dbset.Where(where).FirstOrDefault();
}
protected override void DisposeCore()
{
if (_dataContext != null)
_dataContext.Dispose();
}
}
It fails in this method
public TEntity Get(Expression<Func<TEntity, bool>> #where)
{
return Dbset.Where(where).FirstOrDefault(); <<<<<< Fails Here <<<<<
}
I was on the right track and it was because I was inheriting from the Base Entity.
When I re read my problem it became clear.
The solution was to simply not to inherit from base entity class and all is good.

Custom Widget In Orchard CMS Throw error

**Hi Friends,
I am try to create custom widget in Orchard to display Student detail it show in widget list in admin panel but throw error when click on save button when i try to use it.it shows error
error is:-
And my code is
Model Code is:-
public class studentPart :ContentPart<studentPartRecord>
{
public string Rollno { get { return Record.Rollno; } set { Record.Rollno =value; } }
public string Name { get { return Record.Name; } set { Record.Name = value; } }
public string Class { get { return Record.Class; } set { Record.Class = value; } }
}
public class studentPartRecord :ContentPartRecord
{
public virtual string Rollno { get; set; }
public virtual string Name { get; set; }
public virtual string Class { get; set; }
}
Migration code is:-
public int Create() {
// Creating table tb_Student_studentPartRecord
SchemaBuilder.CreateTable("tb_Student_studentPartRecord", table =>table
.ContentPartRecord()
.Column("Rollno", DbType.String)
.Column("Name", DbType.String)
.Column("Class", DbType.String)
);
return 1;
}
public int UpdateFrom1()
{
// Creating table tb_EmpData_EmpDataPartRecord
ContentDefinitionManager.AlterPartDefinition(typeof(studentPart).Name,
builder => builder.Attachable());
ContentDefinitionManager.AlterTypeDefinition("StudentWidget",
cfg => cfg
.WithPart("studentPart")
.WithPart("WidgetPart")
.WithPart("CommonPart")
.WithPart("IdentityPart")
.WithSetting("Stereotype", "Widget"));
return 2;
}
Driver code is:-
public class studentPartDriver :ContentPartDriver<studentPart>
{
protected override DriverResult Display(studentPart part, string displayType, dynamic shapeHelper)
{
return ContentShape("Parts_student",
() => shapeHelper.Parts_student(Rollno:part.Rollno,Name:part.Name,Class:part.Class));
}
//GET
protected override DriverResult Editor(studentPart part, dynamic shapeHelper)
{
return ContentShape("Parts_student_Edit",
() => shapeHelper.EditorTemplate(TemplateName: "Parts/student", Model: part, Prefix: Prefix));
}
//POST
protected override DriverResult Editor(studentPart part, IUpdateModel updater, dynamic shapeHelper)
{
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part, shapeHelper);
}
}
Handler Code is:-
public class studentPartHandler :ContentHandler
{
public studentPartHandler(IRepository<studentPartRecord> repository)
{
Filters.Add(StorageFilter.For(repository));
Filters.Add(new ActivatingFilter<studentPart>("student"));
}
}
Please help me . Thanks in Advance
Change studentPart to StudentPart
Change studentPartRecord to StudentPartRecord
Change SchemaBuilder.CreateTable("tb_Student_studentPartRecord" to SchemaBuilder.CreateTable("StudentPartRecord"
As Bertrand says, your class names should be pascal case to comply with C# conventions, and the table name you pass to CreateTable should be the same as the record's class name. Orchard takes care of prefixing the final database table for you.

Fluent validation with dynamic message

I am trying to building custom validation with dynamic message in fluent validation library.
For example :
public class CreateProcessValidator : AbstractValidator<CreateProcessVM>
{
public CreateProcessValidator()
{
RuleFor(x => x.ProcessFile).Must((x,e) => IsProcessFileValid(x.ProcessFile))).WithMessage("Parse failed with error : {0}");
}
public bool IsProcessFileValid(HttpPostedFileBase file)
{
var errorMessage = "..." // pass result to validaton message ?
// logic
return false;
}
}
Is here any workaround how to pass validation result ?
Thanks
Have you tried something like this?
public class IsProcessFileValid : PropertyValidator
{
public IsProcessFileValid(): base("{ValidationMessage}") {}
protected override IsValid(PropertyValidatorContext context)
{
if (!IsProcessFileValid1(context))
context.MessageFormatter.AppendArgument("ValidationMessage",
"Custom validation message #1");
if (!IsProcessFileValid2(context))
context.MessageFormatter.AppendArgument("ValidationMessage",
"Custom validation message #2");
// ...etc
return true;
}
private bool IsProcessFileValid1(PropertyValidatorContext context)
{
// logic
return false;
}
private bool IsProcessFileValid2(PropertyValidatorContext context)
{
// logic
return false;
}
// ...etc
}
With extension method:
public static class IsProcessFileValidExtensions
{
public static IRuleBuilderOptions<T, object> MustBeValidProcessFile<T>
(this IRuleBuilder<T, object> ruleBuilder)
{
return ruleBuilder.SetValidator(new IsProcessFileValid());
}
}
... and then use it without a custom WithMessage:
public CreateProcessValidator()
{
RuleFor(x => x.ProcessFile).MustBeValidProcessFile();
}
By creating a custom PropertyValidator, you can encapsulate the default validation message within that class and make it dynamic. However you must not use the .WithMessage extension when declaring the RuleFor, because that would override the default validation message which you customized directly inside the PropertyValidator.
There's no way to do that. I would split the complex validation method you currently have into smaller methods (IsProcessFileValid1, IsProcessFileValid2, IsProcessFileValid3, ...) so that you could have more fine grained control over the error message. Also each method will be responsible for validating only once concern making them more reusable (single responsibility):
RuleFor(x => x.ProcessFile)
.Must(IsProcessFileValid1)
.WithMessage("Message 1")
.Must(IsProcessFileValid2)
.WithMessage("Message 2")
.Must(IsProcessFileValid3)
.WithMessage("Message 3");
Also notice how I simplified the lambda as the method could directly be passed to Must as argument.
Here is how I solved it. Tested with FluentValidation v8.5.0
class EmptyValidationMessage : IStringSource
{
public string ResourceName => null;
public Type ResourceType => null;
public string GetString(IValidationContext context)
{
return string.Empty;
}
public static readonly EmptyValidationMessage Instance = new EmptyValidationMessage();
}
public class MyPropValidator : PropertyValidator
{
public MyPropValidator() : base(EmptyValidationMessage.Instance)
{
}
protected override bool IsValid(PropertyValidatorContext context)
{
// if not valid
Options.ErrorMessageSource = new StaticStringSource("my message");
// you can do LanguageStringSource, LazyStringSource, LocalizedStringSource, etc
// example with localized string (https://github.com/clearwaterstream/LocalizedString.FluentValidation)
Options.ErrorMessageSource = new LocalizedStringSource("my message").InFrench("moi message");
return false;
}
}
Faced the same issue, while trying to insert exception message into WithMessage().
It worked with the method overload taking Func<T, string> messageProvider as parameter.
Here is the solution presented on the posters example (working code, FluentValidation v 9.1):
public class CreateProcessVM
{
public object ProcessFile { get; set; }
}
public class CreateProcessValidator : AbstractValidator<CreateProcessVM>
{
public CreateProcessValidator()
{
var message = "Something went wrong.";
RuleFor(x => x.ProcessFile)
.Must((x, e) => IsProcessFileValid(x.ProcessFile, out message))
// .WithMessage(message); will NOT work
.WithMessage(x => message); //Func<CreateProcessVM, string> as parameter
}
public bool IsProcessFileValid(object file, out string errorMessage)
{
errorMessage = string.Empty;
try
{
Validate(file);
return true;
}
catch (InvalidOperationException e)
{
errorMessage = e.Message;
return false;
}
}
private void Validate(object file)
{
throw new InvalidOperationException("File of type .custom is not allowed.");
}
}
And a test demonstrating that we really get the exception message in the error message:
[Fact]
public void Test()
{
var validator = new CreateProcessValidator();
var result = validator.Validate(new CreateProcessVM());
Assert.False(result.IsValid);
Assert.Equal("File of type .custom is not allowed.", result.Errors[0].ErrorMessage);
}

attribute dependent on another field

In a model of my ASP.NET MVC application I would like validate a textbox as required only if a specific checkbox is checked.
Something like
public bool retired {get, set};
[RequiredIf("retired",true)]
public string retirementAge {get, set};
How can I do that?
Thank you.
Take a look at this: http://blogs.msdn.com/b/simonince/archive/2010/06/04/conditional-validation-in-mvc.aspx
I've modded the code somewhat to suit my needs. Perhaps you benefit from those changes as well.
public class RequiredIfAttribute : ValidationAttribute
{
private RequiredAttribute innerAttribute = new RequiredAttribute();
public string DependentUpon { get; set; }
public object Value { get; set; }
public RequiredIfAttribute(string dependentUpon, object value)
{
this.DependentUpon = dependentUpon;
this.Value = value;
}
public RequiredIfAttribute(string dependentUpon)
{
this.DependentUpon = dependentUpon;
this.Value = null;
}
public override bool IsValid(object value)
{
return innerAttribute.IsValid(value);
}
}
public class RequiredIfValidator : DataAnnotationsModelValidator<RequiredIfAttribute>
{
public RequiredIfValidator(ModelMetadata metadata, ControllerContext context, RequiredIfAttribute attribute)
: base(metadata, context, attribute)
{ }
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
// no client validation - I might well blog about this soon!
return base.GetClientValidationRules();
}
public override IEnumerable<ModelValidationResult> Validate(object container)
{
// get a reference to the property this validation depends upon
var field = Metadata.ContainerType.GetProperty(Attribute.DependentUpon);
if (field != null)
{
// get the value of the dependent property
var value = field.GetValue(container, null);
// compare the value against the target value
if ((value != null && Attribute.Value == null) || (value != null && value.Equals(Attribute.Value)))
{
// match => means we should try validating this field
if (!Attribute.IsValid(Metadata.Model))
// validation failed - return an error
yield return new ModelValidationResult { Message = ErrorMessage };
}
}
}
}
Then use it:
public DateTime? DeptDateTime { get; set; }
[RequiredIf("DeptDateTime")]
public string DeptAirline { get; set; }
Just use the Foolproof validation library that is available on Codeplex:
https://foolproof.codeplex.com/
It supports, amongst others, the following "requiredif" validation attributes / decorations:
[RequiredIf]
[RequiredIfNot]
[RequiredIfTrue]
[RequiredIfFalse]
[RequiredIfEmpty]
[RequiredIfNotEmpty]
[RequiredIfRegExMatch]
[RequiredIfNotRegExMatch]
To get started is easy:
Download the package from the provided link
Add a reference to the included .dll file
Import the included javascript files
Ensure that your views references the included javascript files from within its HTML for unobtrusive javascript and jquery validation.
Using NuGet Package Manager I intstalled this: https://github.com/jwaliszko/ExpressiveAnnotations
And this is my Model:
using ExpressiveAnnotations.Attributes;
public bool HasReferenceToNotIncludedFile { get; set; }
[RequiredIf("HasReferenceToNotIncludedFile == true", ErrorMessage = "RelevantAuditOpinionNumbers are required.")]
public string RelevantAuditOpinionNumbers { get; set; }
I guarantee you this will work!
I have not seen anything out of the box that would allow you to do this.
I've created a class for you to use, it's a bit rough and definitely not flexible.. but I think it may solve your current problem. Or at least put you on the right track.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
namespace System.ComponentModel.DataAnnotations
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class RequiredIfAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{0}' is required";
private readonly object _typeId = new object();
private string _requiredProperty;
private string _targetProperty;
private bool _targetPropertyCondition;
public RequiredIfAttribute(string requiredProperty, string targetProperty, bool targetPropertyCondition)
: base(_defaultErrorMessage)
{
this._requiredProperty = requiredProperty;
this._targetProperty = targetProperty;
this._targetPropertyCondition = targetPropertyCondition;
}
public override object TypeId
{
get
{
return _typeId;
}
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, _requiredProperty, _targetProperty, _targetPropertyCondition);
}
public override bool IsValid(object value)
{
bool result = false;
bool propertyRequired = false; // Flag to check if the required property is required.
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
string requiredPropertyValue = (string) properties.Find(_requiredProperty, true).GetValue(value);
bool targetPropertyValue = (bool) properties.Find(_targetProperty, true).GetValue(value);
if (targetPropertyValue == _targetPropertyCondition)
{
propertyRequired = true;
}
if (propertyRequired)
{
//check the required property value is not null
if (requiredPropertyValue != null)
{
result = true;
}
}
else
{
//property is not required
result = true;
}
return result;
}
}
}
Above your Model class, you should just need to add:
[RequiredIf("retirementAge", "retired", true)]
public class MyModel
In your View
<%= Html.ValidationSummary() %>
Should show the error message whenever the retired property is true and the required property is empty.
Hope this helps.
Try my custom validation attribute:
[ConditionalRequired("retired==true")]
public string retirementAge {get, set};
It supports multiple conditions.

Resources