Pass multiple ajax data to form using asp.net core razor pages.

huangapple go评论52阅读模式
英文:

Pass multiple ajax data to form using asp.net core razor pages

问题

I am trying to pass jquery data values to another page's form. I've tried the suggested solutions I found on the net but still no luck.

On my Screenshot, the solution I am thinking is to add the data to my href and from there extract the values and add them to my fields. Here is the code I am trying but failed:

$(document).ready(function () {
    dataTable = $('#DT_employees').DataTable({
        "pageLength": 50,
        "ajax": {
            "url": "/api/Employees",
            "type": "GET",
            "datatype": "json"
        },
        "columns": [
            { "data": "employeeId", "width": "10%" },
            { "data": "userName", "width": "10%" },
            { "data": "firstName", "width": "10%" },
            { "data": "lastName", "width": "10%" },
            {
                "data": "{employeeId, firstName, lastName}",
                "render": function (data) {
                    return `<div class="w-75 btn-group">
                                <a href="/Trainees/Index?=${data}" class="btn btn-primary text-white">
                                </a>
                            </div>`
                },
                "width": "5%"
            }
        ],
        "width": "100%"
    });
});
英文:

I am trying to pass jquery data values to another page's form. I've tried the suggested solutions I found in the net but still no luck.

On my Screenshot, the solution I am thinking is to add the data to my href and from there extract the values and add to my fields. Here is the code I am trying but failed:

$(document).ready(function () {
    dataTable = $(&#39;#DT_employees&#39;).DataTable({
        &quot;pageLength&quot;: 50,
        &quot;ajax&quot;: {
            &quot;url&quot;: &quot;/api/Employees&quot;,
            &quot;type&quot;: &quot;GET&quot;,
            &quot;datatype&quot;: &quot;json&quot;
        },
        &quot;columns&quot;: [
            { &quot;data&quot;: &quot;employeeId&quot;, &quot;width&quot;: &quot;10%&quot; },
            { &quot;data&quot;: &quot;userName&quot;, &quot;width&quot;: &quot;10%&quot; },
            { &quot;data&quot;: &quot;firstName&quot;, &quot;width&quot;: &quot;10%&quot; },
            { &quot;data&quot;: &quot;lastName&quot;, &quot;width&quot;: &quot;10%&quot; },
            {
                &quot;data&quot;: &quot;{employeeId, firstName, lastName}&quot;,
                &quot;render&quot;: function (data) {
                    return `&lt;div class=&quot;w-75 btn-group&quot;&gt;
                                &lt;a href = &quot;/Trainees/Index?=${data}&quot; class=&quot;btn btn-primary text-white&quot;&gt;
                                &lt;/a&gt;
                            &lt;/div&gt;`
                },
                &quot;width&quot;: &quot;5%&quot;
            }
        ],
        &quot;width&quot;: &quot;100%&quot;
    });
});

答案1

得分: 0

以下是您提供的代码的翻译部分:

我创建了一个示例,但我使用了DataTable的`full`属性,您可以将其用作参考。

模型:

public class Employee
{
    public int employeeId { get; set; }
    public string userName { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
}

HomeController

public IActionResult Index()
{
    return View();
}

public IActionResult Employees()
{
    List&lt;Employee&gt; employees = new List&lt;Employee&gt;()
    {
        new Employee() { employeeId=1,userName=&quot;Tom Smith&quot;,firstName=&quot;Smith&quot;,lastName=&quot;Tom&quot;},
        new Employee() { employeeId=2,userName=&quot;Jim Willer&quot;,firstName=&quot;Willer&quot;,lastName=&quot;Jim&quot;}
    };
    return Json(employees);
}
(HomeController)Index.cshtml

&lt;table id=&quot;DT_employees&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;employeeId&lt;/th&gt;
            &lt;th&gt;userName&lt;/th&gt;
            &lt;th&gt;firstName&lt;/th&gt;
            &lt;th&gt;lastName&lt;/th&gt;
            &lt;th&gt;Details&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
&lt;/table&gt;

&lt;script&gt;
    $(document).ready(function () {
        dataTable = $(&#39;#DT_employees&#39;).DataTable({
            &quot;pageLength&quot;: 50,
            &quot;ajax&quot;: {
                &quot;url&quot;: &quot;/Home/Employees&quot;,
                &quot;type&quot;: &quot;GET&quot;,
                &quot;datatype&quot;: &quot;json&quot;,
                &quot;dataSrc&quot;: function (d) {
                     return d
                  }
            },
            &quot;columns&quot;: [
                { &quot;data&quot;: &quot;employeeId&quot;, &quot;name&quot;: &quot;employeeId&quot;, &quot;width&quot;: &quot;10%&quot; },
                { &quot;data&quot;: &quot;userName&quot;, &quot;name&quot;: &quot;userName&quot;, &quot;width&quot;: &quot;10%&quot; },
                { &quot;data&quot;: &quot;firstName&quot;,&quot;name&quot;: &quot;firstName&quot;, &quot;width&quot;: &quot;10%&quot; },
                { &quot;data&quot;: &quot;lastName&quot;, &quot;name&quot;: &quot;lastName&quot;, &quot;width&quot;: &quot;10%&quot; },
                {
                    &quot;render&quot;: function (data,type,full,meta) {
                        return `&lt;div class=&quot;w-75 btn-group&quot;&gt;
                                    &lt;a href = &quot;/Trainees/Index?employeeId=${full.employeeId}&amp;firstName=${full.firstName}&amp;lastName=${full.lastName}&quot; class=&quot;btn btn-primary text-white&quot;&gt;details
                                    &lt;/a&gt;
                                &lt;/div&gt;`
                    },
                    &quot;width&quot;: &quot;5%&quot;
                }
            ],
            &quot;width&quot;: &quot;100%&quot;
        });
    });
&lt;/script&gt;

TraineesController

public IActionResult Index(Employee employee)
{
    return View(employee);
}
(TraineesController)Index.cshtml

@model Project.Models.Employee

&lt;div&gt;
    &lt;dl class=&quot;row&quot;&gt;
        &lt;dt class = &quot;col-sm-2&quot;&gt;
            @Html.DisplayNameFor(model =&gt; model.employeeId)
        &lt;/dt&gt;
        &lt;dd class = &quot;col-sm-10&quot;&gt;
            @Html.DisplayFor(model =&gt; model.employeeId)
        &lt;/dd&gt;
        &lt;dt class = &quot;col-sm-2&quot;&gt;
            @Html.DisplayNameFor(model =&gt; model.firstName)
        &lt;/dt&gt;
        &lt;dd class = &quot;col-sm-10&quot;&gt;
            @Html.DisplayFor(model =&gt; model.firstName)
        &lt;/dd&gt;
        &lt;dt class = &quot;col-sm-2&quot;&gt;
            @Html.DisplayNameFor(model =&gt; model.lastName)
        &lt;/dt&gt;
        &lt;dd class = &quot;col-sm-10&quot;&gt;
            @Html.DisplayFor(model =&gt; model.lastName)
        &lt;/dd&gt;
    &lt;/dl&gt;
&lt;/div&gt;

测试结果:

[![enter image description here][1]][1]

[![enter image description here][2]][2]

请注意,我已经省略了代码中的HTML标签,以便在文本中显示代码。如果您需要完整的代码,请将其从上面的翻译中复制并粘贴到您的编辑器中。

英文:

I made an example, but I use the full property of DataTable, you can use it as a reference.

Model:

public class Employee
{
public int employeeId { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
}

HomeController:

public IActionResult Index()
{
return View();
}
public IActionResult Employees()
{
List&lt;Employee&gt; employees = new List&lt;Employee&gt;()
{
new Employee() { employeeId=1,userName=&quot;Tom Smith&quot;,firstName=&quot;Smith&quot;,lastName=&quot;Tom&quot;},
new Employee() { employeeId=2,userName=&quot;Jim Willer&quot;,firstName=&quot;Willer&quot;,lastName=&quot;Jim&quot;}
};
return Json(employees);
}

(HomeController)Index.cshtml:

&lt;table id=&quot;DT_employees&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;employeeId&lt;/th&gt;
&lt;th&gt;userName&lt;/th&gt;
&lt;th&gt;firstName&lt;/th&gt;
&lt;th&gt;lastName&lt;/th&gt;
&lt;th&gt;Details&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;/table&gt;
&lt;script&gt;
$(document).ready(function () {
dataTable = $(&#39;#DT_employees&#39;).DataTable({
&quot;pageLength&quot;: 50,
&quot;ajax&quot;: {
&quot;url&quot;: &quot;/Home/Employees&quot;,
&quot;type&quot;: &quot;GET&quot;,
&quot;datatype&quot;: &quot;json&quot;,
&quot;dataSrc&quot;: function (d) {
return d
}
},
&quot;columns&quot;: [
{ &quot;data&quot;: &quot;employeeId&quot;, &quot;name&quot;: &quot;employeeId&quot;, &quot;width&quot;: &quot;10%&quot; },
{ &quot;data&quot;: &quot;userName&quot;, &quot;name&quot;: &quot;userName&quot;, &quot;width&quot;: &quot;10%&quot; },
{ &quot;data&quot;: &quot;firstName&quot;,&quot;name&quot;: &quot;firstName&quot;, &quot;width&quot;: &quot;10%&quot; },
{ &quot;data&quot;: &quot;lastName&quot;, &quot;name&quot;: &quot;lastName&quot;, &quot;width&quot;: &quot;10%&quot; },
{
&quot;render&quot;: function (data,type,full,meta) {
return `&lt;div class=&quot;w-75 btn-group&quot;&gt;
&lt;a href = &quot;/Trainees/Index?employeeId=${full.employeeId}&amp;firstName=${full.firstName}&amp;lastName=${full.lastName}&quot; class=&quot;btn btn-primary text-white&quot;&gt;details
&lt;/a&gt;
&lt;/div&gt;`
},
&quot;width&quot;: &quot;5%&quot;
}
],
&quot;width&quot;: &quot;100%&quot;
});
});
&lt;/script&gt;

TraineesController:

public IActionResult Index(Employee employee)
{
return View(employee);
}

(TraineesController)Index.cshtml:

@model Project.Models.Employee
&lt;div&gt;
&lt;dl class=&quot;row&quot;&gt;
&lt;dt class = &quot;col-sm-2&quot;&gt;
@Html.DisplayNameFor(model =&gt; model.employeeId)
&lt;/dt&gt;
&lt;dd class = &quot;col-sm-10&quot;&gt;
@Html.DisplayFor(model =&gt; model.employeeId)
&lt;/dd&gt;
&lt;dt class = &quot;col-sm-2&quot;&gt;
@Html.DisplayNameFor(model =&gt; model.firstName)
&lt;/dt&gt;
&lt;dd class = &quot;col-sm-10&quot;&gt;
@Html.DisplayFor(model =&gt; model.firstName)
&lt;/dd&gt;
&lt;dt class = &quot;col-sm-2&quot;&gt;
@Html.DisplayNameFor(model =&gt; model.lastName)
&lt;/dt&gt;
&lt;dd class = &quot;col-sm-10&quot;&gt;
@Html.DisplayFor(model =&gt; model.lastName)
&lt;/dd&gt;
&lt;/dl&gt;
&lt;/div&gt;

Test Result:

Pass multiple ajax data to form using asp.net core razor pages.

Pass multiple ajax data to form using asp.net core razor pages.

huangapple
  • 本文由 发表于 2023年4月17日 17:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定