简体   繁体   中英

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
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.
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:

 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.

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.

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. Here's more info if you need: https://www.learnentityframeworkcore.com/conventions/one-to-many-relationship

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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