简体   繁体   English

如何使用(Jquery)或(Jquery和Ajax)在MVC Core中将数据插入到明细表中

[英]How to insert data to details Table in mvc core using (Jquery) or (Jquery and Ajax)

old school c# developer here.. long story short, the company I'm working for has decided to continue developing using core technology and as a former winforms developer I'm not familiar with the web concepts and having trouble saving the details table to the master table. 长话短说,我正在工作的公司决定继续使用核心技术进行开发,作为前winforms开发人员,我不熟悉Web概念,并且无法将详细信息表保存到主表。 Any help is very appreciated. 任何帮助都非常感谢。

I have two Tables with 1 to many Relationship 我有两个表,一对多关系

TOURNAMENTS_M (Master)

TOURNAMENTS_D (Detail) 

I have edited the scaffolded Create page to include the fields for the detail table for entry. 我已经编辑了脚手架的“创建”页面,以包括要输入的明细表的字段。

I have even managed to update the table(visually) for the added details records using Jquery. 我什至设法使用Jquery(可视地)为添加的详细信息记录更新了表。

I'm having trouble to save this information to my database though, there have been a couple of different tutorials I've been following to with different approaches.. yet could't manage to succeed using neither approaches... 我在将这些信息保存到数据库时遇到了麻烦,但是我一直在使用不同的方法来学习一些不同的教程。但是,使用这两种方法都无法成功...

1st Approach is using Jquery and Ajax method. 第一种方法是使用Jquery和Ajax方法。 I've seen examples where people save data through a modal window and when save is clicked the data is updated on the gridview so as I understood(correct me if I'm wrong) Ajax is used to reload data without refreshing the page, and to postback the data to the controller. 我已经看到了一些示例,这些示例中的人们通过模式窗口保存数据,并且单击保存时会在gridview上更新数据,因此据我所知(如果我错了,请纠正我)Ajax用于重新加载数据而不刷新页面,并且将数据回传到控制器。 In my case I'm no sure if this is the right method to use since both master and child controls are on the same page. 就我而言,我不确定这是否是正确的方法,因为主控件和子控件都在同一页面上。

2nd Approach is using just Jquery 第二种方法只是使用jQuery

in this approach my Create method in the controller and jquery methods are as following 在这种方法中,我在控制器中的Create方法和jquery方法如下

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(TOURNAMENTS_M pTournamentsM)
    {

        if (ModelState.IsValid)
        {
            _context.Add(pTournamentsM);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(pTournamentsM);
    }

function GetCurrentDetail() {

var TD_LEVEL = $("#TD_LEVEL").val();
var TD_SB = $("#TD_SB").val();
var TD_BB = $("#TD_BB").val();


var tourDetail = {
    "TD_LEVEL": TD_LEVEL,
    "TD_SB": TD_SB,
    "TD_BB": TD_BB
};

return tourDetail;

} }

//triggers when clicked on add tour details button function CreateRowForTourDetails() { //点击“添加游览详细信息”按钮功能时的触发功能CreateRowForTourDetails(){

    var current = GetCurrentDetail();
    var index = $("#detailsTable").children("tr").length;


    var indexcell = "<td style='display:none'> <input type='hidden' id='index" + index + "' name='TOURNAMENTS_D.index' value = '" + index + "' /> </td>";

    var TD_LEVEL = "<td> <input type='hidden' id='TD_LEVEL" + index + "' name='TOURNAMENTS_D[" + index + "].TD_LEVEL ' value = '" + current.TD_LEVEL + "' />" + current.TD_LEVEL + " </td>";
    var TD_SB =    "<td> <input type='hidden' id='TD_SB" + index + "' name='TOURNAMENTS_D[" + index + "].TD_SB ' value = '" + current.TD_SB + "' />" + current.TD_SB + " </td>";
    var TD_BB =    "<td> <input type='hidden' id='TD_BB" + index + "' name='TOURNAMENTS_D[" + index + "].TD_BB ' value = '" + current.TD_BB + "' />" + current.TD_BB + " </td>";

    //var removeButton = "<td><a id ='myRemove' data-itemId='0'  class='btn btn-primary'>Remove</a></td>";

    var tourDetail = "<tr>" +indexcell +  +TD_LEVEL + TD_SB + TD_BB  + "</tr>";

    var detailsTableBody = $("#DetailsTableBody");

    detailsTableBody.append(tourDetail);     
}

with this method when I place my break point inside the Create function in my controller I can see that the count of TOURNAMENTS_D increases however the value is null 通过这种方法,当我将断点放置在控制器的Create函数中时,我可以看到TOURNAMENTS_D的计数增加了,但是该值为null 在此处输入图片说明

Try Web Browser Debug tool to check the request, make sure your request like below: 尝试使用Web Browser Debug工具检查请求,确保您的请求如下所示:

在此处输入图片说明

Here is My Model: 这是我的模特:

    public class Order
{
    public int Id { get; set; }
    public string OrderNo { get; set; }
    public ICollection<OrderDetail> OrderDetails { get; set; }
}

public class OrderDetail
{
    public int Id { get; set; }
    public int ProductId { get; set; }
}

暂无
暂无

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

相关问题 How to insert HTML table data into SQL Server in a single batch by using jquery ajax and structured parameter with ASP.NET MVC controller - How to insert HTML table data into SQL Server in a single batch by using jquery ajax and structured parameter with ASP.NET MVC controller 如何在ASP.NET MVC中使用jQuery / Ajax将数据插入数据库? - How to insert data into a database using jQuery / Ajax in ASP.NET MVC? Jquery/Ajax 更新表[ASP.NET Core MVC] - Jquery/Ajax to update a TABLE[ASP.NET Core MVC] 在 ASP.NET Core MVC 应用程序中使用 ajax JQuery 呈现后,JSON 数据返回为未定义 - JSON data is returned as undefined after rendered using ajax JQuery in ASP.NET Core MVC app 使用 ASP.NET 6.0 MVC 应用程序中的 jQuery AJAX 将数据插入数据库 - Insert data into database using jQuery AJAX in ASP.NET 6.0 MVC Application 如何使用ajax jquery将多个对象从视图发布到dot net core 6 MVC控制器 - How To Post Multiple Objects from View To dot net core 6 MVC Controller using ajax jquery 使用jquery ajax发布和获取数据,仅在mvc中更新html表行 - post and get data using jquery ajax and update html table row only in mvc jquery数据表如何填充ajax请求 - jquery data table how to populate on ajax request 使用JQuery Ajax将控制器数据作为段落插入div中 - Insert controller data inside a div as a paragraph using JQuery ajax 在ASP.NET MVC和AJAX jQuery数据表中使用数据注释 - Using Data Annotations in ASP.NET MVC with AJAX jQuery Datatables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM