Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework SelectList from ViewModel from repository Asp.net core_Entity Framework_Asp.net Core Mvc_Viewmodel_Repository Pattern - Fatal编程技术网

Entity framework SelectList from ViewModel from repository Asp.net core

Entity framework SelectList from ViewModel from repository Asp.net core,entity-framework,asp.net-core-mvc,viewmodel,repository-pattern,Entity Framework,Asp.net Core Mvc,Viewmodel,Repository Pattern,这是我的第一篇帖子,所以请小心点。。 我使用的是VS2017,asp.net core 2.2 使用存储库模式在db menaging ViewModel中保存对象的方法有问题。 我正试图用VM保存表提供中表类别的id。我想将“提供”视图中的类别显示为DropDownList中的NameCategory,并将值Id保存到“提供”表中。这是我的文件: 类别: public class Offers { public int Id { get; set; }

这是我的第一篇帖子,所以请小心点。。 我使用的是VS2017,asp.net core 2.2 使用存储库模式在db menaging ViewModel中保存对象的方法有问题。 我正试图用VM保存表提供中表类别的id。我想将“提供”视图中的类别显示为DropDownList中的NameCategory,并将值Id保存到“提供”表中。这是我的文件:

类别:

 public class Offers
    {
        public int Id { get; set; }

        public string Title{ get; set; }            
        public string Contents{ get; set; }
        public int CategoryId { get; set; }
        public Category Categorys { get; set; }
        public string PicturePath { get; set; }
    }

 public class Category
    {
        [Key]
        public int CategoryId { get; set; }
        public string NameCategory { get; set; }
        public IEnumerable<Offers> OffersList { get; set; }
    }
视图:

@model offerSVM
@{
ViewBag.Title=“添加报价。”;
}
标题
选择类别。
请选择类别。
内容提供。
图画
加价
我不知道如何使用SelectList或List来提供Id类别。如果每个人都知道一些关于父亲的好故事,或者(更好)能帮助解决我的问题,我将不胜感激。 亚当

附言
对不起,我的英语…

asp项目之后
应该是
SelectList
List
类型

这是一个可行的解决方案

首先,您可以对您的
报价器VM
进行一些更改:

offerSVM
class:

 public class OffeersVM
{
    public string Title { get; set; }
    public string Content { get; set; }
    public int CategoryId { get; set; }
    public List<Category> Category { get; set; }
    public List<IFormFile> Picture { get; set; }
}
我模拟了一段数据进行测试:

 var vm = new OffeersVM
        {
            Title = "New OfersVM",
            Content = "A OfferVM",
            Category = new List<Category>
            {
               new Category
               {
                   CategoryId=1,
                   NameCategory="AA",
                   OffersList=new List<Offers>
                   {
                       new Offers{Id=1,CategoryId=1,Contents="few",Title="fdfw"},
                       new Offers{Id=2,CategoryId=1,Contents="sdgsdg",Title="gsdg"},
                   }
               },
              new Category
              {
                   CategoryId=2,
                   NameCategory="BB",
                   OffersList=new List<Offers>
                   {
                       new Offers{Id=3,CategoryId=2,Contents="hghg",Title="fdfw"},
                       new Offers{Id=4,CategoryId=2,Contents="sddfj",Title="gsdg"},
                   }
              }
            }
var vm=新报价人vm
{
Title=“新OfersVM”,
Content=“A OfferVM”,
类别=新列表
{
新类别
{
类别ID=1,
NameCategory=“AA”,
OfferList=新列表
{
新优惠{Id=1,CategoryId=1,Contents=“now”,Title=“fdfw”},
新优惠{Id=2,CategoryId=1,Contents=“sdgsdg”,Title=“gsdg”},
}
},
新类别
{
类别ID=2,
NameCategory=“BB”,
OfferList=新列表
{
新优惠{Id=3,CategoryId=2,Contents=“hghg”,Title=“fdfw”},
新优惠{Id=4,CategoryId=2,Contents=“sddfj”,Title=“gsdg”},
}
}
}
结果:

Hi@AaddaammJJ,有关于这个案子的最新消息吗?
@model OffeersVM
 
@{
    ViewBag.Title = "Add offer.";
}

<form asp-controller="Home" asp-action="Create" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label asp-for="Title">Title</label>
        <input asp-for="Title" class="form-control" id="exampleFormControlInput1" placeholder="Title">
        <span asp-validation-for="Title" class="text-danger"></span>
    </div>

    <div class="form-group">
        <label for="exampleFormControlSelect1">Select category.</label>
        <select class="form-control" id="exampleFormControlSelect1" asp-for="Category" asp-items="@*I't know what item...?*@">
            <option value=" test">Please select category.</option>
        </select>

        <span asp-validation-for="Category"></span>
    </div>

    <div class="form-group">
        <label for="exampleFormControlTextarea1" asp-for="Content">Content offer.</label>
        <textarea class="form-control" id="exampleFormControlTextarea1" rows="6" asp-for="Content"></textarea>
        <span asp-validation-for="Content" class="text-danger"></span>
    </div>

    <div class="form-group">
       
        <div class="col-sm-10">

            <div class="custom-file">
                <input multiple asp-for="Picture" class="form-control custom-file-input " />
                <label class="custom-file-label ">Picture</label>
            </div>
       </div>
    </div>


    <div>
        <button type="submit" class="btn btn-outline-success">Add offer</button>
    </div>
    
</form>
 public class OffeersVM
{
    public string Title { get; set; }
    public string Content { get; set; }
    public int CategoryId { get; set; }
    public List<Category> Category { get; set; }
    public List<IFormFile> Picture { get; set; }
}
 <select class="form-control" id="exampleFormControlSelect1" asp-for="CategoryId" asp-items="@(new SelectList(Model.Category,"CategoryId","NameCategory"))">
        <option value=" test">Please select category.</option>
    </select>
 var vm = new OffeersVM
        {
            Title = "New OfersVM",
            Content = "A OfferVM",
            Category = new List<Category>
            {
               new Category
               {
                   CategoryId=1,
                   NameCategory="AA",
                   OffersList=new List<Offers>
                   {
                       new Offers{Id=1,CategoryId=1,Contents="few",Title="fdfw"},
                       new Offers{Id=2,CategoryId=1,Contents="sdgsdg",Title="gsdg"},
                   }
               },
              new Category
              {
                   CategoryId=2,
                   NameCategory="BB",
                   OffersList=new List<Offers>
                   {
                       new Offers{Id=3,CategoryId=2,Contents="hghg",Title="fdfw"},
                       new Offers{Id=4,CategoryId=2,Contents="sddfj",Title="gsdg"},
                   }
              }
            }