sách gpt4 ai đã đi

javascript - 如何将数组从 JavaScript 传递到我的操作方法

In lại 作者:行者123 更新时间:2023-11-29 22:18:00 hai mươi bốn 4
mua khóa gpt4 Nike

我的 ASP.Net mvc 3 Web 应用程序中有以下 View :-

@model IEnumerable

@{
ViewBag.Title = "customer";
}

Customers Info










但我面临一个问题,我需要通过单击“下一页”将 moveTo 中的所有记录传递给 customersDetials 操作方法 Action 链接。所以任何人都可以建议一种方法,我可以使用这种方法将 moveto 中的项目从我上面的 View 传递到我使用 html.actionlink 的操作方法。

:::UPDATE:::

我已将以下内容添加到 View 中:-

@using (Html.BeginForm("CustomersDetials", "Home"))
{







然后我创建了以下 ViewModel 类来挖掘所选客户的值(value):-

public class SelectedCustomers
{
public IEnumerable Info { get; set; }
}

然后我添加了以下操作方法:-

   public ActionResult CustomersDetials(string[] SelectRight)
{
foreach (var item in SelectRight) {

// how to assign the values of the array to my viewmodel object
}

但我无法确定如何在我的操作方法中将数组的值分配给 viewmodel 对象。

:::UPDATE2:::

这就是我得到的结果。操作方法如下所示:-

    public ActionResult CustomersDetails(string[] SelectRight)
{
var selectedCustomers = new SelectedCustomers
{
Info = SelectRight.Select(GetAccount)
};


return View(selectedCustomers);

}
private Account GetAccount(string id)
{


return entities.Account.Find(id);
}
}

以及以下 View :-

@model MvcApplication4.Models.SelectedCustomers

@{
ViewBag.Title = "CustomerDetials";
}

CustomerDetials


//code goes here

@foreach (var item in Model.Info) {


@Html.DisplayFor(modelItem => item.ORG_ID)

但是当我运行该应用程序时,出现了以下错误:-

The type of one of the primary key values did not match the type defined in the entity. See inner exception for details.

关于:-

return entities.Account.Find(id);

::已解决::我在操作方法和 GETAccount 函数中都将字符串更改为很长。

1 Câu trả lời

您可以将 Next 链接设为表单的提交按钮。然后当提交表单时,它会自动将选择列表中的选定项目发送到服务器。这是我推荐给你的解决方案。只需将选择列表放入一个表单中,也不要忘记为您的选择列表命名,因为这就是您将在 Controller 操作中用于检索所选值的名称:

@using (Html.BeginForm("CustomersDetials", "Home"))
{




}

顺便说一句,您可以使用内置的 Html.ListBoxFor 帮助程序,而不是在您的 View 中执行那些可怕的 foreach 循环来生成那些多个选择框。

现在您的 Controller 操作可能如下所示:

[HttpPost]
public ActionResult CustomersDetials(string[] selectRight)
{
... the selectRight parameter will contain the selected values in the
corresponding select list
}

处理这种情况的另一种可能性是使用 AJAX:

@Html.ActionLink("Next Page", "CustomersDetials", "Home", null, new { id = "next" })

然后:

$('#next').click(function() {
$.ajax({
url: this.href,
type: 'POST',
contentType: 'application/json;charset=utf-8',
data: JSON.stringify(selectedItems),
success: function(data) {

}
});
trả về false;
});

当然,您需要使 selectedItems 成为全局变量,因为现在您已经在 MoveRight、MoveLeft 按钮的点击处理程序中声明了它。否则,将无法在下一个链接的点击事件中访问此变量。


更新 1:

根据您的更新,您可以像这样填充您的 View 模型:

public ActionResult CustomersDetials(string[] selectRight)
{
var selectedCustomers = new SelectedCustomers
{
Info = selectRight.Select(GetAccount)
};
... use your view model here
}

private Account GetAccount(string id)
{
// TODO: given the selected ID go and fetch the corresponding
// Account instance and return it here:

return new Account
{
Id = id
};
}

关于javascript - 如何将数组从 JavaScript 传递到我的操作方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14013138/

hai mươi bốn 4 0
Bài viết được đề xuất: Nguyên lý $http.post của Angularjs
Bài viết được đề xuất: java - Android 工作线程卡住主线程
Bài viết được đề xuất: android - 无法从内部存储读取文件(文件不存在问题)
Bài viết được đề xuất: javascript - 面无背
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com