1

I have 2 simple models:

public class Country
{    
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Region> Region { get; set; }
}

public partial class Region
{    
    public int ID { get; set; }
    public string Name { get; set; }
    public int CountryID { get; set; }

    public virtual Country Country { get; set; }
}

Is it possible to have a single page to handle the creation of a country whereby the user inputs the country with multiple regions and then only posts to the server?

I've seen an implementation here where you create a custom ViewModel with numbered properties (Region1, Region2, Region3, etc) but it's limiting, any suggestions?

(I know AngularJS can be used to do this however I have no experience in this space as of yet.)

Thanks

Community
  • 1
  • 1
Gareth
  • 243
  • 1
  • 5
  • 12

2 Answers2

0

Yes its very possible it just depends on how you plan to implement this.

My favourite style of implementing One to Many pages is initially creating the "one" (country) then redirecting to a page with a grid element where users can add the many (regions) to the one. It works well and its a very easy way for both the programmer to create and the user to understand.

As for creating a country with multiple regions in a single post, it could be done but you must think of how the implementation will work.

DotNet NF
  • 805
  • 1
  • 6
  • 14
  • So would this be like a tabbed layout whereby you proceed through the tabs? How would such a Grid Element work? Thanks for the fast response. – Gareth Oct 17 '14 at 05:43
0

Sure, this is easy to do. You have defined your data model. Either you use that also as your View Model, or you can create a new model that is a complex object. The methods in your type:

public virtual Country Country { get; set; }
public virtual ICollection<Region> Region { get; set; }

These method being present normally indicates you're using Entity Framework and that these are "related entities" that you can traverse via this "navigation property" at run-time. You can create a Country and populate the Region collection on the fly when you try to use it.

Here is a good example of using a View Model: What is ViewModel in MVC?

///Example of a Controller method creating a view model to display
    [HttpGet]
    public ActionResult Index()
    {
        var user = _userService.Get(User.Identity.Name);
        var customerId = GlobalDataManager.GetCustomerId();

        if (_error != null)
        {
            ModelState.AddModelError("", _error);
            _error = null;
        }

        var model = new InboundListModel();
        model.Initialize(customerId, user.CompanyId);

        foreach (var campaign in model.Campaigns)
        {
            model.InitializeCallProggress(campaign.Id, _callInfoService.GetCallsCount(campaign.Id));
        }

        return View(model);
    }

This View Model can be anything you want but it does need to be one type. So if you want 2 put 2 types in the ViewModel you just need a new container object:

    public class ComplexViewModel
    {
        public Country Country { get; set; }
        public ICollection<Region> Regions { get; set; }
    }

Then you just need a way to populate the data like the example above where I call Initialize. This goes out to EF via a DAL project and retrieves the data for the model.

LinkedListT
  • 681
  • 8
  • 24
Gavin Stevens
  • 673
  • 5
  • 14
  • I understand the concept of the ViewModel however, I don't know how to create multiple regions and bind them to the ICollection property of the viewmodel before posting back to the server. (Thanks for the fast response.) – Gareth Oct 17 '14 at 05:52