0

My ApplicationController:

public class ApplicationController : Controller
{
   private IUxRepository _repository;
   public ApplicationController(IUxRepository repository)
   {
     _repository = repository;
   }

   public ActionResult Create()
   {
      return View("Shape", new ApplicationViewModel());
   }
}

My ApplicationViewModel

public class ApplicationViewModel : ViewModelBase
{
    public ApplicationViewModel()
    {
        Application = new Application();
    }

    public Application Application {get;set;}
}

My Application Model:

public class Application : DbEntity
{
    public string Name {get;set;}

    [Display(Name = "Proposed Release Date"),
     RegularExpression(@"(^Q[1-4])\s(2\d{3})", ErrorMessage = "Date needs to be in the format Q{1-4}{space}20{YY} e.g. Q4 2013 or Q1 2014")]
    public string ProposedReleaseDate {get;set;}
}

Extract from Shape view:

<div class="editor-label">
@Html.DisplayFor(model => model.ProposedReleaseDate)
</div>

<div class="editor-field">
@Html.EditorFor(model => model.ProposedReleaseDate)
@Html.ValidationMessageFor(model => model.ProposedReleaseDate)
</div>

When I try and load up my Shape view, for some reason it comes back with a validation error saying the ProposedReleaseDate doesn't meet the required RegularExpression.

Of course it doesn't because it's a new entity waiting for the input, why is it validating before the page has loaded up. Its validating too early. How do I bypass this, or where do I turn this feature off, its counter intuitive/productive IMHO.

Callum Linington
  • 14,213
  • 12
  • 75
  • 154
  • Your class inheritance seems a bit odd. Shouldn't the validation and display attributes be placed on your ApplicationViewModel not on you entity class? – Jammer May 21 '13 at 15:49
  • Ummm, not that I know off. When you're building code first you either decorate with attributes, or use fluent api. I decorate with attributes. It doesn't affect the database. It does provide a single place to put validation. I prefer it either way to be on the model itself containing it all in one place. It doesn't mess the code up at all. – Callum Linington May 21 '13 at 15:53
  • Is it a validation error or "Input string was not in a correct format" exception? – LostInComputer May 21 '13 at 15:55
  • Ahh yes, it was "Input string was not in the correct format" – Callum Linington May 21 '13 at 15:56
  • @No1_Melman I validate at the UI level not in my entities, by the time the data gets to an entity it should have been validated and deemed correct. What good is a `Display` attribute on an entity for instance? – Jammer May 21 '13 at 16:24
  • When you build up a view around it, I get it to base it on the Model, then it will build around the attributes I've set it. Rather than have two sources. It does validate it against the model, then sends it to the database. – Callum Linington May 21 '13 at 19:33
  • @Jammer could you please point to your source of information, I'm intrigued to see how it is done and if I can learn anything, thanks – Callum Linington May 22 '13 at 09:28
  • Not really a source of information just the way I work. I consider a `ViewModel` by it's very name to be a specialized version of something else or a generally specific class with a strict use case for UI specific representation of something. For instance your `Display` attribute on an Entity class would be pointless for me. It's never going anywhere near a UI in order to have anything to display.`ApplicationViewModel` should have `ProposedReleaseDate` property on it and THAT would have the `Display` attribute on it, not the `Application` entity class. – Jammer May 22 '13 at 11:13
  • @Jammer So you would copy your `Model`s out into your `ViewModel`s? To me, that just seems a bit counterproductive, I want the `ViewModel` to have references to the `Model`, because that is what I think a `ViewModel` is for, to mediate between the Model and View, so it knows `Model` and the `View` knows the `ViewModel`. I just use it for when i'm using the templates, but when the template has used the `Model`, I then change the `@model` statement to use the `ViewModel` rather than the model. Its similar to what you suggest. – Callum Linington May 22 '13 at 13:12
  • Not necessarily, the `ViewModel` can simply wrap the `Model` so the `ProposedReleaseDate` on the `ViewModel`might be `ProposedReleaseDate {get {return _application.ProposedReleaseDate;}}` The point is that you do UI stuff in a `ViewModel` not in an Entity class. I've done lots of MVVM in the past with WPF and this is what works best for me. – Jammer May 22 '13 at 13:51

1 Answers1

1

The ErrorMessage format is not correct. Instead of

ErrorMessage = "Date needs to be in the format Q{1-4}{space}20{YY} e.g. Q4 2013 or Q1 2014"

make it

ErrorMessage = "Date needs to be in the format Q{{1-4}}{{space}}20{{YY}} e.g. Q4 2013 or Q1 2014"

Note that "{" is now "{{" and "}" is now "}}"

Edit: related link How to escape braces (curly brackets) in a format string in .NET

Community
  • 1
  • 1
LostInComputer
  • 15,188
  • 4
  • 41
  • 49