4
\$\begingroup\$
Email<input type="text" id="email" /><br />
Re-enter Email<input type="text" id="confirm-email" />

I'm thinking that I want a model so that I can have this partial page strongly typed. But what a lame model....

It would look like this, and if this is a good Idea, what should I name this model?

public class ConfirmEmailModel?????
{
    [Required]
    [Display(Name = "Email",ResourceType=typeof(mvc3test.Resources.Common))]
    public string Email{ get; set; }

    [Display(Name = "Confirm Email")]
    [Compare("Email", ErrorMessage = "no match.")]
    public string ConfirmEmail{ get; set; }
}
\$\endgroup\$

4 Answers 4

4
\$\begingroup\$

call it a "view model" as its not really a model, its just a model for the view you are showing.

ConfirmEmailViewModel :)

\$\endgroup\$
2
\$\begingroup\$

I don't know ASP.NET, but isn't this the job of a view helper? Or an "HTML Helper" as they put it.

An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML and tags. You also can use HTML Helpers to render more complex content such as a tab strip or an HTML table of database data.

I say don't create a "view model," instead create a view helper.

That is straight from: http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs

\$\endgroup\$
1
\$\begingroup\$

No I don't believe it is overkill. I've felt the same about view models for simple things in the past but it adds bits which are helpful, such as strongly typing and validation.

As Keith, I'd also call it ConfirmEmailViewModel.

\$\endgroup\$
0
\$\begingroup\$

I like to take an MVVM style approach to MVC, for example, I use:

public class Person { }

as my domain model, and

public class PersonViewModel { }

as my view model. The view model is expressly used with the Controller and View. I also tend to use a mapping framework quite a lot, so I might mock up something like

public PersonController(IViewModelService<PersonViewModel> viewModelService)
{
    _viewModelService = viewModelService;
}

public ViewResult Edit(int id) 
{
    PersonViewModel model = _viewModelService.Get(id);
    return View(model);
}

[HttpPost]
public ViewResult Edit(PersonViewModel model)
{
    if (ModelState.Valid)
    {
        _viewModelService.Update(model);
    }

    return View(model);
}

where the IViewModelService<PersonViewModel> service is responsible for mapping my view model back to my domain model Person.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.