{"id":16,"date":"2012-02-20T19:30:16","date_gmt":"2012-02-20T17:30:16","guid":{"rendered":"http:\/\/dev.bratched.fr\/en\/?p=16"},"modified":"2012-02-20T19:30:16","modified_gmt":"2012-02-20T17:30:16","slug":"sample-mvc-project-updated","status":"publish","type":"post","link":"https:\/\/bratched.com\/en\/2012\/02\/20\/sample-mvc-project-updated\/","title":{"rendered":"Sample ASP.Net MVC project (updated)"},"content":{"rendered":"<p>I\u2019ve updated the <a href=\"http:\/\/bratched.com\/en\/home\/aspnet-mvc\/62-how-we-do-aspnet-mvc.html\" target=\"_blank\" rel=\"noopener noreferrer\">sample MVC project<\/a> I wrote for using ASP.NET MVC 3 and the Razor view engine.<\/p>\n<p>A minor change is that I no longer use the FluentValidationModelValidatorProvider but the standard one. So I decorate the view model with the necessary data annotations :<\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:39b2e7ac-d477-4b7b-8b06-6fee077190d6\">\n<pre class=\"lang:c# decode:true\">[Validator(typeof(UserViewModelValidator))]\npublic class UserViewModel\n{\n    public int Id { get; set; }\n\n    [DisplayName(\"First name *\")]\n    public string FirstName { get; set; }\n\n    [DisplayName(\"Last name *\")]\n    public string LastName { get; set; }\n\n    public int? Age { get; set; }\n}<\/pre>\n<\/div>\n<p><!--more--><\/p>\n<p>And the validator becomes :<\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2ea7938e-0494-4b8f-827a-6d167a2743af\">\n<pre class=\"lang:default decode:true\">public class UserViewModelValidator : AbstractValidator&lt;UserViewModel&gt;\n{\n    public UserViewModelValidator()\n    {\n        RuleFor(x =&gt; x.FirstName)\n            .NotEmpty()\n            .WithMessage(\"First name is required\");\n\n        RuleFor(x =&gt; x.LastName)\n            .NotEmpty()\n            .WithMessage(\"Last name is required\");\n    }\n}<\/pre>\n<\/div>\n<p>And here are the corresponding views using Razor :<\/p>\n<p><strong>Index.cshtml<\/strong><\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fcf9a5ac-a3ae-4760-a115-a040122f2c7f\">\n<pre class=\"lang:default decode:true\">@model IEnumerable&lt;SampleMvc.Web.Models.UserViewModel&gt;\n\n@{\n    ViewBag.Title = \"Index\";\n}\n\n&lt;h2&gt;Index&lt;\/h2&gt;\n@(Html\n    .Grid&lt;UserViewModel&gt;(Model)\n    .Columns(column =&gt; {\n        column.Custom(model =&gt; Html.Partial(\"_TableLinks\", model));\n        column.For(model =&gt; model.FirstName);\n        column.For(model =&gt; model.LastName);\n        column.For(model =&gt; model.Age);\n    })\n)\n\n&lt;p&gt;\n    @(Html.ActionLink&lt;UsersController&gt;(c =&gt; c.New(), \"Create New\"))\n&lt;\/p&gt;<\/pre>\n<\/div>\n<p><strong>_TableLinks.cshtml<\/strong><\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:be003ee2-842e-478a-a468-2e7a38981854\">\n<pre class=\"lang:default decode:true\">@model SampleMvc.Web.Models.UserViewModel\n\n@(Html.ActionLink&lt;UsersController&gt;(c =&gt; c.Edit(Model.Id), \"Edit\")) |\n@(Html.ActionLink&lt;UsersController&gt;(c =&gt; c.Show(Model.Id), \"Details\")) |\n@using (Html.BeginForm&lt;UsersController&gt;(c =&gt; c.Destroy(Model.Id))) \n{\n    @Html.HttpMethodOverride(HttpVerbs.Delete)\n    &lt;input type=\"submit\" value=\"Delete\" \/&gt;\n}<\/pre>\n<\/div>\n<p><strong>Edit.cshtml<\/strong><\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0233224d-b1d1-4fe0-ae8f-be72e793676d\">\n<pre class=\"lang:default decode:true\">@model SampleMvc.Web.Models.UserViewModel\n@{\n    ViewBag.Title = \"Edit\";\n}\n\n&lt;h2&gt;Edit&lt;\/h2&gt;\n@using (Html.BeginForm&lt;UsersController&gt;(c =&gt; c.Update(null)))\n{\n    @Html.ValidationSummary(true)\n    @Html.HttpMethodOverride(HttpVerbs.Put)\n    @Html.HiddenFor(model =&gt; model.Id)\n    @Html.EditorForModel()\n    &lt;p&gt;\n        &lt;input type=\"submit\" value=\"Save\" \/&gt;\n    &lt;\/p&gt;\n}\n&lt;div&gt;\n    @(Html.ActionLink&lt;UsersController&gt;(c =&gt; c.Index(), \"Back to List\"))\n&lt;\/div&gt;<\/pre>\n<\/div>\n<p><strong>New.cshtml<\/strong><\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:43c4f90f-3dad-4c7c-86ee-7c2bdaf1b3a2\">\n<pre class=\"lang:c# decode:true crayon-selected\">@model SampleMvc.Web.Models.UserViewModel\n\n@{\n    ViewBag.Title = \"New\";\n}\n\n&lt;h2&gt;New&lt;\/h2&gt;\n@using (Html.BeginForm&lt;UsersController&gt;(c =&gt; c.Create(null))) \n{\n    @Html.ValidationSummary(true)\n    @Html.EditorForModel()\n    &lt;p&gt;\n        &lt;input type=\"submit\" value=\"Create\" \/&gt;\n    &lt;\/p&gt;\n}\n&lt;div&gt;\n    @(Html.ActionLink&lt;UsersController&gt;(c =&gt; c.Index(), \"Back to List\"))\n&lt;\/div&gt;<\/pre>\n<\/div>\n<p><strong>Show.cshtml<\/strong><\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:79dc337e-141e-4091-9b8f-b384b57e949f\">\n<pre class=\"lang:default decode:true\">@model SampleMvc.Web.Models.UserViewModel\n\n@{\n    ViewBag.Title = \"Show\";\n}\n\n&lt;h2&gt;Show&lt;\/h2&gt;\n\n@Html.DisplayForModel()\n&lt;p&gt;\n    @(Html.ActionLink&lt;UsersController&gt;(c =&gt; c.Edit(Model.Id), \"Edit\")) |\n    @(Html.ActionLink&lt;UsersController&gt;(c =&gt; c.Index(), \"Back to List\"))\n&lt;\/p&gt;<\/pre>\n<\/div>\n<p><strong>UserViewModel.cshtml editor template<\/strong><\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cfb6445a-cdcb-49ca-ad00-0d5a90a87b45\">\n<pre class=\"lang:default decode:true\">@model SampleMvc.Web.Models.UserViewModel\n\n&lt;fieldset&gt;\n    &lt;legend&gt;Fields&lt;\/legend&gt;\n\n    &lt;div class=\"editor-label\"&gt;\n        @Html.LabelFor(model =&gt; model.FirstName)\n    &lt;\/div&gt;\n    &lt;div class=\"editor-field\"&gt;\n        @Html.EditorFor(model =&gt; model.FirstName)\n        @Html.ValidationMessageFor(model =&gt; model.FirstName)\n    &lt;\/div&gt;\n\n    &lt;div class=\"editor-label\"&gt;\n        @Html.LabelFor(model =&gt; model.LastName)\n    &lt;\/div&gt;\n    &lt;div class=\"editor-field\"&gt;\n        @Html.EditorFor(model =&gt; model.LastName)\n        @Html.ValidationMessageFor(model =&gt; model.LastName)\n    &lt;\/div&gt;\n\n    &lt;div class=\"editor-label\"&gt;\n        @Html.LabelFor(model =&gt; model.Age)\n    &lt;\/div&gt;\n    &lt;div class=\"editor-field\"&gt;\n        @Html.EditorFor(model =&gt; model.Age)\n        @Html.ValidationMessageFor(model =&gt; model.Age)\n    &lt;\/div&gt;\n&lt;\/fieldset&gt;<\/pre>\n<\/div>\n<p><strong>UserViewModel.cshtml display template<\/strong><\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f28543fb-e6df-46c8-b096-5bee71365596\">\n<pre class=\"lang:default decode:true\">@model SampleMvc.Web.Models.UserViewModel\n\n&lt;fieldset&gt;\n    &lt;legend&gt;Fields&lt;\/legend&gt;\n\n    &lt;div class=\"display-label\"&gt;Id&lt;\/div&gt;\n    &lt;div class=\"display-field\"&gt;@Html.DisplayFor(x =&gt; x.Id)&lt;\/div&gt;\n\n    &lt;div class=\"display-label\"&gt;FirstName&lt;\/div&gt;\n    &lt;div class=\"display-field\"&gt;@Html.DisplayFor(x =&gt; x.FirstName)&lt;\/div&gt;\n\n    &lt;div class=\"display-label\"&gt;LastName&lt;\/div&gt;\n    &lt;div class=\"display-field\"&gt;@Html.DisplayFor(x =&gt; x.LastName)&lt;\/div&gt;\n\n    &lt;div class=\"display-label\"&gt;Age&lt;\/div&gt;\n    &lt;div class=\"display-field\"&gt;@Html.DisplayFor(x =&gt; x.Age)&lt;\/div&gt;\n&lt;\/fieldset&gt;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve updated the sample MVC project I wrote for using ASP.NET MVC 3 and the Razor view engine. A minor change is that I no longer use the FluentValidationModelValidatorProvider but the standard one. So I decorate the view model with the necessary data annotations : [Validator(typeof(UserViewModelValidator))] public class UserViewModel { public int Id { get; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[263,265],"tags":[275,304,315],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":0,"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bratched.com\/en\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}