Sample MVC project (updated)

Envoyer Imprimer PDF
Note des utilisateurs: / 20
MauvaisTrès bien 
There are no translations available.

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>Indexh2>
@(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>Edith2>
@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>Newh2>
@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>Showh2>

@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>Fieldslegend>
            
    <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>Fieldslegend>
        
    <div class="display-label">Iddiv>
    <div class="display-field">@Html.DisplayFor(x => x.Id)div>
        
    <div class="display-label">FirstNamediv>
    <div class="display-field">@Html.DisplayFor(x => x.FirstName)div>
        
    <div class="display-label">LastNamediv>
    <div class="display-field">@Html.DisplayFor(x => x.LastName)div>
        
    <div class="display-label">Agediv>
    <div class="display-field">@Html.DisplayFor(x => x.Age)div>
fieldset>

 

The source code is available on github.

Mise à jour le Dimanche, 20 Février 2011 11:33