如何在ASP.NET MVC razor视图中验证hiddenfor字段? [英] How do I validate a hiddenfor field in ASP.NET MVC razor view?
问题描述
我的剃刀视图中有一个jQuery可选小部件,它显示为一个并在SQL Server表中用逗号分隔的字符串更新,如下所示:
< ol 类 = ui-selectable 样式 = width:auto id = selectable1 >
@ {
var color =型号.AvailableColors.Split( '');
foreach(var clr in color)
{
< li class = btn red-mint style = margin:10px 0 > @clr < / li >
}
}
< / ol >
上面提到的上面显示了一组颜色,如(红色,绿色,紫色等)列表当然是可选择的,用户一次只能从列表中选择一个。
我通过d隐藏字段中选定的列表项值,然后通过以下脚本传递给控制器操作方法。
< 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 })
现在,我遇到了困难,找不到通过互联网搜索的解决方案。我希望如果没有选择任何项目,则应该进行验证,并且应该出现AvailableColors的验证消息,但我不知道该怎么做。有什么帮助吗?
请注意我将值从视图传递到另一个控制器操作方法,这是操作方法。
< br $>
public ActionResult AddToCart( int id, int SelectedQuantity, string SelectedSizes, string AvailableColors)
{
// 从数据库中检索相册
var addedProduct = dbContext.Products
。 Single (产品= > product.ProductID == id);
// 将其添加到购物车
var cart = ShoppingCart.GetCart( this .HttpContext);
cart.AddToCart(addedProduct,SelectedQuantity,AvailableColors,SelectedSizes);
// 返回主商店页面以获取更多购物
return RedirectToAction( 索引 );
}
我的尝试:
我尝试使用[必需]数据注释将另一个属性添加到我的视图模型中。
公共类SizeColorViewModel
{
[HiddenInput(DisplayValue = false)]
[Key]
public int ProductID {get;组; }
[显示(名称=选择尺寸)]
[必填(ErrorMessage =请选择尺寸)]
public string AvailableSizes {get;组; }
[必填]
public string SelectedSizes {get;组; }
}
但即使我无法验证该字段有什么想法吗?
(文件).ready(function(){
(#selectable1)。selectable({
selected:function(event,ci){
(ci.selected).siblings()removeClass( UI选择);
I have a jQuery selectable widget in my razor view which displayed as an and updated from a comma separated string in SQL Server table just like below:
<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>
The above as mentioned displays set of colors like (Red, Green, Purple and etc) list which is of-course selectable and the user can only pick one from the list at a time.
I passed the selected list item value in a hidden field which is then passed to controller action method with below script.
<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>
In my form I've a HiddenFor razor attribute to pass selected list item value to the controller as below:
@Html.HiddenFor(m => m.AvailableColors, new { @id = "selectedcolor" })
Now here is something that I am stuck and couldn't find a solution searching over internet. I want that if no item selected the validation should occur and the validation message for the AvailableColors should appear but I have no idea how to do it. Any help please?
Please note that I am passing values from view to another controller action method, here is the action method.
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");
}
What I have tried:
I tried to add another property to my view model as below with [Required] Data annotation.
public class SizeColorViewModel
{
[HiddenInput(DisplayValue = false)]
[Key]
public int ProductID { get; set; }
[Display(Name="Select Size")]
[Required(ErrorMessage="Please select a size")]
public string AvailableSizes { get; set; }
[Required]
public string SelectedSizes { get; set; }
}
But even that i couldn't validate that field any idea?
(document).ready(function () {
("#selectable1").selectable({ selected: function (event, ci) {
(ci.selected).siblings().removeClass("ui-selected");
这篇关于如何在ASP.NET MVC razor视图中验证hiddenfor字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!