HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

How to access and share MVC Model?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mvchowshareandmodelaccess

Problem

I have the following model:

namespace Site.Models.Country
{
    public class Country
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
        public string CountryUrl { get; set; }
    }
}


In a separate model folder for a different view and controller, I have a different model like this:

namespace Site.Models.Directory
{
    public class DirectoryProfileView
    {
        public List Countries { get; set; }

        public DirectoryProfileView()
        {
            this.Countries = new List(Country.GetCountryRegions());
        }
    }
}


Is it correct for me to use the Country model from a different model ? I tried adding using Site.Models.Country;

However for the following to work:

public List Countries { get; set; }


I need to call by

public List Countries { get; set; }


My two questions are:

-
Is this correct? I don't really want to be creating another model exactly like my first Country model.

-
Also, any feedback on my naming conventions would be appreciated too.

Solution

I think you need to read up a little on namespaces. Both of your namespaces don't make sense unless you will have multiple types of countries and directories.

Your namespace should be if you will have multiple countries

namespace Site.Models.Countries


otherwise it should be

namespace Site.Models


In regards to naming your view models, I would think on how you will use these models in future. If you are likely to have a page for creating, updating, removing and viewing the country, then you might well have the following view models:

  • DisplayCountry



  • UpdateCountry



  • RemoveCountry



  • SaveCountry



Some people choose to reuse single country model, but I've seen this cause more trouble than benefit. For example, in the RemoveCountry model I'll have only two properties:

public int Id { get; set; }
public string Name { get; set;


The first property is purely for data removal purposes - I assume that you'll pass id to your data layer eventually. Second property is mainly for better UX journey. You might use it for notifying user that they are about to remove a country. E.g., in your view you might have

Warning
Are you sure you want to remove @Model.Name

Code Snippets

namespace Site.Models.Countries
namespace Site.Models
public int Id { get; set; }
public string Name { get; set;
<h2>Warning</h2>
<p>Are you sure you want to remove @Model.Name</p>

Context

StackExchange Code Review Q#30364, answer score: 3

Revisions (0)

No revisions yet.