Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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

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
C# 如何将数据传递给Controller-一对多关系.net核心_C#_Entity Framework_Asp.net Core - Fatal编程技术网

C# 如何将数据传递给Controller-一对多关系.net核心

C# 如何将数据传递给Controller-一对多关系.net核心,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,我想将这些数据保存到数据库中 有两个模型。运动员和客户 关系是客户有很多运动员 我想知道如何通过运动员详细信息传递客户详细信息 public class Athlete { [Key] public string ID { get; set; } = Guid.NewGuid().ToString(); public string FirstName { get; set; } public string LastName { get; set; } pu

我想将这些数据保存到数据库中

有两个模型。运动员和客户

关系是客户有很多运动员

我想知道如何通过运动员详细信息传递客户详细信息

public class Athlete
{
    [Key]
    public string ID { get; set; } = Guid.NewGuid().ToString();
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Description { get; set; }
    public Client Client { get; set; }
}



public class Client
{
    [Key]
    public string ID { get; set; } = Guid.NewGuid().ToString();
    public string CompanyName { get; set; }
    public string ApplicationUserId { get; set; }
    public List<Athlete> Athletes { get; set; }
}
公共级运动员
{
[关键]
公共字符串ID{get;set;}=Guid.NewGuid().ToString();
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串电子邮件{get;set;}
公共字符串说明{get;set;}
公共客户端{get;set;}
}
公共类客户端
{
[关键]
公共字符串ID{get;set;}=Guid.NewGuid().ToString();
公共字符串CompanyName{get;set;}
公共字符串applicationSerID{get;set;}
公开名单运动员{get;set;}
}
//这是HTML表单

<li>
        <label>First Name</label>
        @Html.TextBoxFor(x => x.FirstName, new { maxlength = 100 })
    </li>
    <li>
        <label>Last Name</label>
        @Html.TextBoxFor(x => x.LastName, new { maxlength = 100 })
    </li>
    <li>
        <label>Email</label>
        @Html.TextBoxFor(x => x.Email, new { maxlength = 100 })
    </li>
    <li>
        <label>Address Line 3</label>
         @Html.TextBoxFor(x => x.AddressLine3, new { maxlength = 100 })
    </li>
</ul>
  • 名字 @TextBoxFor(x=>x.FirstName,新的{maxlength=100})
  • 姓 @TextBoxFor(x=>x.LastName,新的{maxlength=100})
  • 电子邮件 @TextBoxFor(x=>x.Email,新的{maxlength=100})
  • 地址行3 @TextBoxFor(x=>x.AddressLine3,新的{maxlength=100})
  • 只要这样做:

            .
            .
            .
            <li>
                <label>CompanyName</label>
                @Html.TextBoxFor(x => x.Client.CompanyName )
            </li>
            <li>
                <label>ApplicationUserId</label>
                @Html.TextBoxFor(x => x.Client.ApplicationUserId)
            </li>
           <li>
               //and another field you want...
            </li>
        </ul>
    
    。
    .
    .
    
  • 公司名称 @Html.TextBoxFor(x=>x.Client.CompanyName)
  • 应用程序系列 @Html.TextBoxFor(x=>x.Client.applicationSerid)
  • //还有另一个你想要的领域。。。
  • 希望对您有所帮助。

    您可以使用这个软件包。可以找到演示。下面是示例代码 内部控制器

            public IActionResult Index()
            {
                return View();
            }
            [AjaxOnly]
            [ResponseCache(NoStore = true, Duration = 0)]
            public IActionResult NewItem()
            {
                return PartialView("_CollectionPartial", new OrderItemModel());
            }
    
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create( Client client)
     {
            if (ModelState.IsValid)
            {
                _context.Add(client);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(client);
      }
    
    在视图中

        @foreach (var item in Model.OrderItemModel)
                        {
                            <partial name="NewItem.cshtml" model="item" />
                        }
        <script>
         $('#btnAddMore').click(function () {
                    $.ajax({
                        url: '/Home/NewItem',
                        success: function (html) {
                            $('#collectionItems').append(html);
                        }
                    });
                });
    
    </script>
    
    @foreach(Model.OrderItemModel中的变量项)
    {
    }
    $('#btnAddMore')。单击(函数(){
    $.ajax({
    url:“/Home/NewItem”,
    成功:函数(html){
    $('#collectionItems')。附加(html);
    }
    });
    });
    
    详细信息部分页

    @using HtmlHelpers.BeginCollectionItemCore;
    @model BeginCollectionItemCoreDemo.ViewModels.OrderItemModel
    
        <div class="form-horizontal well">
            @using (Html.BeginCollectionItem("OrderItems"))
            {
                <div class="form-group">
                    <label asp-for="Id" class="col-md-2 control-label"></label>
                    <div class="col-md-10">
                        <input asp-for="Id" class="form-control" />
                        <span asp-validation-for="Id" class="text-danger" />
                    </div>
                </div>
        }
    
    @使用HtmlHelpers.BeginCollectionItemCore;
    @模型BeginCollectionItemCoreDemo.ViewModels.OrderItemModel
    @使用(Html.BeginCollectionItem(“OrderItems”))
    {
    }
    
    我想知道如何通过运动员详细信息传递客户详细信息

    public class Athlete
    {
        [Key]
        public string ID { get; set; } = Guid.NewGuid().ToString();
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Description { get; set; }
        public Client Client { get; set; }
    }
    
    
    
    public class Client
    {
        [Key]
        public string ID { get; set; } = Guid.NewGuid().ToString();
        public string CompanyName { get; set; }
        public string ApplicationUserId { get; set; }
        public List<Athlete> Athletes { get; set; }
    }
    
    在添加客户端详细信息时,可以使用javascript添加多个运动员详细信息,并更新控制器中的数据库,如下所示:

    模型

    公共类客户端
    {
    [关键]
    公共字符串ID{get;set;}=Guid.NewGuid().ToString();
    公共字符串CompanyName{get;set;}
    公共字符串applicationSerID{get;set;}
    公开名单运动员{get;set;}
    }
    公开课运动员
    {
    [关键]
    公共字符串ID{get;set;}=Guid.NewGuid().ToString();
    公共字符串名{get;set;}
    公共字符串LastName{get;set;}
    公共字符串电子邮件{get;set;}
    公共字符串说明{get;set;}
    //公共客户端{get;set;}
    }
    
    看法

    @model WebApplication1.Models.Clients.Client
    名字
    姓
    电子邮件
    描述
    @节脚本
    {
    $(“#btnAdd”)。单击(函数(e){
    e、 预防默认值();
    变量i=($(“.items”).length)/4;
    变量n=“”+“”+
    '' +
    '' +
    '' +'';
    $(“#项目列表”)。追加(n);
    }
    
    控制器

            public IActionResult Index()
            {
                return View();
            }
            [AjaxOnly]
            [ResponseCache(NoStore = true, Duration = 0)]
            public IActionResult NewItem()
            {
                return PartialView("_CollectionPartial", new OrderItemModel());
            }
    
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create( Client client)
     {
            if (ModelState.IsValid)
            {
                _context.Add(client);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(client);
      }
    
    [HttpPost]
    [ValidateAntiForgeryToken]
    公共异步任务创建(客户端)
    {
    if(ModelState.IsValid)
    {
    _添加(客户端);
    wait_context.SaveChangesAsync();
    返回重定向到操作(名称(索引));
    }
    返回视图(客户端);
    }
    
    将一个Clientdata添加到数据库时,您的意思是要将运动员集合添加到相应的表中吗?