【发布时间】:2016-09-03 17:31:21
【问题描述】:
我的剃刀视图中有一个 jQuery selectable 小部件,它显示为 <ol>,并从 SQL Server 表中的逗号分隔字符串更新,如下所示:
<ol class="ui-selectable" style="width:auto" id="selectable1">
@{
var color = Model.AvailableColors.Split(',');
foreach (var clr in color)
{
<li class="btn red-mint" style="margin: 10px 0">@clr</li>
}
}
</ol>
上面提到的显示一组颜色,如(红色、绿色、紫色等)列表,当然是selectable,用户一次只能从列表中选择一个。
我在隐藏字段中传递了选定的列表项值,然后使用下面的脚本将其传递给控制器操作方法。
<script type="text/javascript">
$(document).ready(function () {
$("#selectable1").selectable({
selected: function (event, ci) {
$(ci.selected).siblings().removeClass("ui-selected");
$("#selectedcolor").val($("#selectable1>li.ui-selected").html());
}
});
});
</script>
在我的表单中,我有一个 HiddenFor razor 属性将选定的列表项值传递给控制器,如下所示:
@Html.HiddenFor(m => m.AvailableColors, new { @id = "selectedcolor" })
现在我遇到了一些问题,无法通过 Internet 搜索找到解决方案。我希望如果没有选择任何项目,则应该进行验证并且应该出现AvailableColors 的验证消息,但我不知道该怎么做。请问有什么帮助吗?
编辑:这是我将数据传递给的控制器操作方法:
// GET: /Store/AddToCart/5
public ActionResult AddToCart(int id,int SelectedQuantity, string SelectedSizes, string AvailableColors)
{
// Retrieve the album from the database
var addedProduct = dbContext.Products
.Single(product => product.ProductID == id);
// Add it to the shopping cart
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.AddToCart(addedProduct, SelectedQuantity, AvailableColors,SelectedSizes);
// Go back to the main store page for more shopping
return RedirectToAction("Index");
}
【问题讨论】:
标签: jquery asp.net-mvc razor