Sample ASP.Net MVC project (updated)

I’ve 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; set; }

    [DisplayName("First name *")]
    public string FirstName { get; set; }

    [DisplayName("Last name *")]
    public string LastName { get; set; }

    public int? Age { get; set; }
}

And the validator becomes :

public class UserViewModelValidator : AbstractValidator<UserViewModel>
{
    public UserViewModelValidator()
    {
        RuleFor(x => x.FirstName)
            .NotEmpty()
            .WithMessage("First name is required");

        RuleFor(x => x.LastName)
            .NotEmpty()
            .WithMessage("Last name is required");
    }
}

And here are the corresponding views using Razor :

Index.cshtml

@model IEnumerable<SampleMvc.Web.Models.UserViewModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@(Html
    .Grid<UserViewModel>(Model)
    .Columns(column => {
        column.Custom(model => Html.Partial("_TableLinks", model));
        column.For(model => model.FirstName);
        column.For(model => model.LastName);
        column.For(model => model.Age);
    })
)

<p>
    @(Html.ActionLink<UsersController>(c => c.New(), "Create New"))
</p>

_TableLinks.cshtml

@model SampleMvc.Web.Models.UserViewModel

@(Html.ActionLink<UsersController>(c => c.Edit(Model.Id), "Edit")) |
@(Html.ActionLink<UsersController>(c => c.Show(Model.Id), "Details")) |
@using (Html.BeginForm<UsersController>(c => c.Destroy(Model.Id))) 
{
    @Html.HttpMethodOverride(HttpVerbs.Delete)
    <input type="submit" value="Delete" />
}

Edit.cshtml

@model SampleMvc.Web.Models.UserViewModel
@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>
@using (Html.BeginForm<UsersController>(c => c.Update(null)))
{
    @Html.ValidationSummary(true)
    @Html.HttpMethodOverride(HttpVerbs.Put)
    @Html.HiddenFor(model => model.Id)
    @Html.EditorForModel()
    <p>
        <input type="submit" value="Save" />
    </p>
}
<div>
    @(Html.ActionLink<UsersController>(c => c.Index(), "Back to List"))
</div>

New.cshtml

@model SampleMvc.Web.Models.UserViewModel

@{
    ViewBag.Title = "New";
}

<h2>New</h2>
@using (Html.BeginForm<UsersController>(c => c.Create(null))) 
{
    @Html.ValidationSummary(true)
    @Html.EditorForModel()
    <p>
        <input type="submit" value="Create" />
    </p>
}
<div>
    @(Html.ActionLink<UsersController>(c => c.Index(), "Back to List"))
</div>

Show.cshtml

@model SampleMvc.Web.Models.UserViewModel

@{
    ViewBag.Title = "Show";
}

<h2>Show</h2>

@Html.DisplayForModel()
<p>
    @(Html.ActionLink<UsersController>(c => c.Edit(Model.Id), "Edit")) |
    @(Html.ActionLink<UsersController>(c => c.Index(), "Back to List"))
</p>

UserViewModel.cshtml editor template

@model SampleMvc.Web.Models.UserViewModel

<fieldset>
    <legend>Fields</legend>

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

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

    <div class="editor-label">
        @Html.LabelFor(model => model.Age)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Age)
        @Html.ValidationMessageFor(model => model.Age)
    </div>
</fieldset>

UserViewModel.cshtml display template

@model SampleMvc.Web.Models.UserViewModel

<fieldset>
    <legend>Fields</legend>

    <div class="display-label">Id</div>
    <div class="display-field">@Html.DisplayFor(x => x.Id)</div>

    <div class="display-label">FirstName</div>
    <div class="display-field">@Html.DisplayFor(x => x.FirstName)</div>

    <div class="display-label">LastName</div>
    <div class="display-field">@Html.DisplayFor(x => x.LastName)</div>

    <div class="display-label">Age</div>
    <div class="display-field">@Html.DisplayFor(x => x.Age)</div>
</fieldset>

 

Leave a comment

Your email address will not be published. Required fields are marked *