简体   繁体   English

ViewModel MVC使用其他模型的信息制作下拉列表

[英]ViewModel MVC making a DropDown List with info from other model

I'm kinda newbie in MVC, so I'm doing a Project in MVC .NET Core to study 我是MVC的新手,所以我正在MVC .NET Core中进行项目研究
I have some difficultes to do a view model 我有一些困难要做一个视图模型

Models: 楷模:

[Table("Pizzas")]
public class Pizza
{
    [Key]
    public int id { get; set; }

    public string Flavour { get; set; }
    public DateTime Date { get; set; }
    public string Size { get; set; }
    public string Deliveryman { get; set; }
}

public class Deliveryman
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
    public string Cellphone { get; set; }
    public string Car { get; set; }
}

I tried some styles of ViewModels but I don't how properly do this. 我尝试了一些样式的ViewModels,但是这样做的方式不正确。
In View to Add Pizzas, I want to Add the name of deliveryman who will deliver the pizzas by a Drop-down list. 在“查看添加披萨”中,我想添加一个要通过下拉列表发送披萨的送货员的名字。
This Project is like a web page to admin pizzas. 该项目就像一个管理比萨饼的网页。

Where are the options of delivery men sources from? 送货人员来源有哪些选择? I'm guessing this is stored in the database and not an enum. 我猜这存储在数据库中,而不是一个枚举。 If that is the case you're viewmodel could look like this: 如果是这种情况,您的视图模型可能如下所示:

public class PizzaViewModel
{
    public int id { get; set; }

    public string Flavour { get; set; }

    public DateTime Date { get; set; }

    public string Size { get; set; }

    public IEnumerable<SelectListItem> Deliverymen { get; set; }

    public int SelectedDeliveryMan { get; set; }
}

where each item in DeliveryMen is built like the following: DeliveryMen中每个项目的构建方式如下:

 new SelectListItem
 {
     Value = individualDeliveryMan.Id,
     Text = individualDeliveryMan.FullName
 });

then in your view, you'll have something like the following: 然后在您看来,您将获得以下内容:

@model YourProject.PizzaViewModel
...
@Html.DropDownListFor(x => x.SelectedDeliveryMan, Model.DeliveryMen)
...

when the data is posted back to your controller, the property SelectedDeliveryMan will hold the Id of the delivery man that was selected in your dropdownlist. 当数据回传到您的控制器时,属性SelectedDeliveryMan将保存在您的下拉列表中选择的送货员的Id

On an unrelated note, I'm guessing Size should be an enum and not a string property as usually there is a predefined set of sizes that a user can choose from instead of allowing users to enter anything they want. 在一个不相关的注释上,我猜想Size应该是一个枚举而不是字符串属性,因为通常会有一组预定义的大小供用户选择,而不是允许用户输入他们想要的任何内容。

You were almost there. 你快到了 From what I understand, one pizza will need only 1 deliveryman to deliver it. 据我了解,一个披萨只需要一名送货员就可以送货。 So you could do something like this: 因此,您可以执行以下操作:

[Table("Pizzas")]
public class Pizza
{
    [Key]
    public int id { get; set; }

    public string Flavour { get; set; }
    public DateTime Date { get; set; }
    public string Size { get; set; }
    public Deliveryman Deliveryman { get; set; }
}

This information when posted to the controller will have all the information you need to figure out who delivered the pizza. 将这些信息发布到控制器后,将具有您需要找出谁提供披萨的所有信息。

If you want to get the reverse information also (find out which pizzas were delivered by a certain deliveryman), you could do something like this: 如果您还想获取反向信息(找出某个送货员送来的披萨),则可以执行以下操作:

public class Deliveryman
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
    public string Cellphone { get; set; }
    public string Car { get; set; }
    public ICollection<Pizza> Pizzas {get; set;}
}

When used in conjunction with powerful ORM like Entity Framework (Core), you can do a whole lot of stuff. 与功能强大的ORM(例如实体框架(Core))结合使用时,您可以做很多事情。 Here's more info if you need: https://www.learnentityframeworkcore.com/conventions/one-to-many-relationship 如果需要,这里有更多信息: https : //www.learnentityframeworkcore.com/conventions/one-to-many-relationship

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

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