0

I came from WPF programming using MVVM, so this is my first experience using MVC in ASP.NET

What I was trying to do is show this model:

public class MultipleChoiceQuestion : Question
{
    public string QuestionText
    public List<MultipleChoiceAnswer> PossibleAnswers { get; set; }
}

public class MultipleChoiceAnswer
{
    public string Text { get; set; }
    public string UrlImage { get; set; }
    public bool IsCorrect { get; set; }
}

As you can see, I have a List<T> and here is where I'm trying to showed in RadioButtons. I was searching some data in this site and I discover that the major use ViewModels in the radioButtons.

Has anyone implement RadioButtonListFor<T> for ASP.NET MVC?

What is the purpose of using ViewModels here? And how can do to show my list in radio buttons? It's just a little bit of confusing.

Community
  • 1
  • 1
Darf Zon
  • 6,268
  • 20
  • 90
  • 149
  • There's a lot of answers on why people use ViewModels (http://stackoverflow.com/questions/664205/viewmodel-best-practices). The most straightforward reason is usually the data your views require is different than the data in your business models. ViewModels are strongly typed and let you aggregate data from different sources. – mfanto Jan 14 '13 at 01:19
  • You may think about a View-models as a trim down aggregation of data objects/entities/models that your View required to have. – Yusubov Jan 14 '13 at 01:28

1 Answers1

5

There's a lot of answers on why people use ViewModels. The most straightforward reason is usually the data your views require is different than the data in your business models. For example:

Let's say your application allows a user to upload an image that then gets saved to some cloud storage like Azure Blob Storage. Files stored in blob storage are accessible via a URL of the form: https://account.blob.core.windows.net/filename.png

Your database model might look something like:

public class Image
{ 
    public int Id { get; set; }
    public string FileName { get; set; }
}

Here, you're saving the file name of the image. It would be a waste of space to save the full URL, plus it would make moving the data to some other location very difficult.

When it comes to displaying the image however, your view needs that full URL. This is where ViewModel's become useful. (See the note below about how to avoid hard coding URL's in ViewModels).

public class ImageViewModel
{
    public int Id { get; set; }
    public string FileName { get; set; }

    public string Url 
    {
        get { return "https://account.blob.core.windows.net/" + FileName; }
    }
}

By passing this to the View, it now has everything it needs to correctly show this image:

<h3>@Model.FileName</h3>
<img src="@Model.Url" alt="@Model.FileName"/>

If you ever move your storage provider, say to Amazon S3 (https://account.s3.amazonaws.com/filename.png), you only need to update the URL in a single place, and your Views are completely unaffected

As a side note, you might want to look at AutoMapper as a way to simplify things even further, and avoid hard coding URL's in your ViewModels. AutoMapper lets you define rules for mapping Models -> ViewModels, including setting default values, flattening objects, etc.

Community
  • 1
  • 1
mfanto
  • 14,168
  • 6
  • 51
  • 61
  • Thank you, I get the idea. Just one doubt, can you me an example using the viewModel in my scenario? just to guide, if it does not bother you – Darf Zon Jan 14 '13 at 01:38
  • It's very tempting to implement logic in getters like above. Be very conservative with this practice. – Dercsár Oct 16 '13 at 18:59