gpt4 book ai didi

javascript - Knockoutjs DropDownList 与 ViewBag

转载 作者:行者123 更新时间:2023-12-03 07:59:48 25 4
gpt4 key购买 nike

我的 ViewBag 中存储了一个 SelectList,我希望使用 Knockout js。

这是迄今为止我在不使用任何 js 的情况下使用 ViewBag 列表时的 View

@model IEnumerable<Site.Models.TicketsOrdered>
<head>
<title>Order</title>
<link rel="stylesheet" href="~/Content/TableSheet.css">
@using GeogSocSite.Models
</head>
<body>
<h1>Choose Your Tickets</h1>
<table align="center" cellspacing="2" border="1" data-bind='visible: gifts().length > 0'>
<thead>
<tr>
<th align="center">Description</th>
<th align="center">Price</th>
<th align="center">Add</th>
</tr>
</thead>
<tbody>
@foreach (Site.Models.Ticket t in ViewBag.listTickets)
{
<tr>
<td align="center">@t.Description</td>
<td align="center">@t.Price</td>
<td align="center">@Html.DropDownList("Quantity", (IList<SelectListItem>)ViewBag.Quantities) </td>

</tr>
}
</tbody>
</table>
<div id="proceed">
@Html.ActionLink("Proceed", "Order", "Order")
</div>
<div>
@Html.ActionLink("Back to List", "Index","Events")
</div>

</body>

我希望能够在单击“继续”按钮时保存从下拉列表中选择的值

我已经查看了 Knockout js 的文档并查看了示例,但许多都引用了级联下拉列表或刚刚在 View 中创建的下拉列表,就像我在这里找到的示例

@model IEnumerable<Site.Models.TicketsOrdered>
<head>
<title>Order</title>
<link rel="stylesheet" href="~/Content/TableSheet.css">
@using GeogSocSite.Models
</head>
<body>
<h1>Choose Your Tickets</h1>
<tr>
<td class="label">Drop-down list:</td>
<td><select data-bind="options: optionValues, value: selectedOptionValue"></select></td>
</tr>
<tr>
<td class="label">Selected option:</td>
<td data-bind="text: selectedOptionValue"></td>
</tr>
<div id="proceed">
@Html.ActionLink("Proceed", "Order", "Order")
</div>
<div>
@Html.ActionLink("Back to List", "Index","Events")
</div>

</body>
<script type="text/javascript">
var viewModel = {
optionValues : ["Alpha", "Beta", "Gamma"],
selectedOptionValue : ko.observable("Gamma"),

};
ko.applyBindings(viewModel);
</script>

如果有人可以帮助我了解如何使用 Knockoutjs 和我的 ViewBag.quantities 列表,以便在单击“继续”时可以保存所选的数量,那就太好了,因为我完全陷入困境了!

最佳答案

您只需将data-bind传递给Html Helper它接受htmlAttributes,所以:

@Html.DropDownList("Quantity", (IList<SelectListItem>)ViewBag.Quantities,"", new {data_bind=" value:  selectedOptionValue"});

你的代码将是这样的:

<h1>Choose Your Tickets</h1>
<tr>
<td class="label">Drop-down list:</td>
<td>@Html.DropDownList("Quantity", (IList<SelectListItem>)ViewBag.Quantities,"", new {data_bind=" value: selectedOptionValue"});</td>
</tr>
<tr>
<td class="label">Selected option:</td>
<td data-bind="text: selectedOptionValue"></td>
</tr>
<div id="proceed">
<a data-bind="attr:{href: "@Url.Action("Order","Order")/"+ selectedOptionValue()}">Proceed</a>
<!-- Or with querystring !-->
<!-- <a data-bind="attr:{href: "@Url.Action("Order","Order")?quantity="+ selectedOptionValue()}">Proceed</a>!-->
</div>
<div>
@Html.ActionLink("Back to List", "Index","Events")
</div>

</body>
<script type="text/javascript">
(function(){
var viewModel = {
selectedOptionValue : ko.observable(),
};
ko.applyBindings(viewModel);
})();
</script>

关于javascript - Knockoutjs DropDownList 与 ViewBag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com