简体   繁体   English

创建具有一对多关系的视图

[英]Create View With One-Many Relationship

I have 2 simple models: 我有2个简单的模型:

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? 我在这里看到了一个实现, 该实现中,您创建了一个具有编号属性(Region1,Region2,Region3等)的自定义ViewModel,但这是有限制的,有什么建议吗?

(I know AngularJS can be used to do this however I have no experience in this space as of yet.) (我知道可以使用AngularJS来做到这一点,但是到目前为止我还没有这个领域的经验。)

Thanks 谢谢

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. 我最喜欢的实现“一对多”页面的样式是最初创建“一个”(国家),然后重定向到带有grid元素的页面,用户可以在其中向页面添加多个(区域)。 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. 至于在一个帖子中创建一个具有多个区域的国家,可以这样做,但是您必须考虑实施的工作方式。

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. 这些存在的方法通常表示您正在使用Entity Framework,并且这些是您可以在运行时通过此“导航属性”遍历的“相关实体”。 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? 这是一个使用视图模型的好例子: 什么是MVC中的ViewModel?

///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: 因此,如果要在ViewModel中放入2个2种类型,则只需要一个新的容器对象:

    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. 这通过DAL项目发送给EF,并检索模型的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM