簡體   English   中英

我在填充下拉列表時遇到問題,該列表的數據在 asp.net mvc 中的不同 model 中引用?

[英]I am having problem in populating dropdown list which has its data referenced in different model in asp.net mvc?

菜單項 model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;
using System.Data.Entity;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using TruYum.Models;

namespace TruYum.Models
{
    public class MenuItem
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public int Price { get; set; }
        [Display(Name = "Free Delivery")]
        [Required]
        public bool FreeDelivery { get; set; }
        [Display(Name = "Date Of Launch")]
        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime Date { get; set; }
        [ForeignKey("Category")]
        [Display(Name = "Category")]
        public int CategoryId { get; set; }
        [Required]
        public bool Active { get; set; }

    }

}

類別 Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Mvc;


namespace TruYum.Models
{
    public class Category
    {
        [Key]
        public int CategoryId { get; set; }
        public string Name { get; set; }
    
    }

}


I have to make a menu items form from MenuItems model in which I have to create a dropdown list with html helper and populate it with the Names from Category Model given is that Categoryid in category is foreign key referenced with category id in menuitems model.

我正在使用 asp.net web 應用程序模板請幫助我是 asp.net 的初學者! 請假設我已經構建了 Dbcontext class 並在類別表中手動輸入或硬編碼了一行。

首先,您應該在 MenuItem 中添加類別 class 作為屬性。

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

    [Required]
    public string Name { get; set; }

    [Required]
    public int Price { get; set; }

    [Display(Name = "Free Delivery")]
    [Required]
    public bool FreeDelivery { get; set; }

    [Display(Name = "Date Of Launch")]
    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime Date { get; set; }

    [Display(Name = "Category")]
    public int CategoryId { get; set; }

    [Required]
    public bool Active { get; set; }

    [ForeignKey(nameof(CategoryId))]
    public Category Category {get; set;}

}

之后,如果您使用實體框架,則在獲取 menuItem 數據的 controller 中,您應該包含類別 class。 像這樣的東西:

var model = context.MenuItems.Include(m => m.Category).toList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM