提问人:AL- MAHROUQI 提问时间:4/1/2023 最后编辑:AL- MAHROUQI 更新时间:6/8/2023 访问量:35
远程验证不适用于模型列表
Remote Validation is not Working for Model List
问:
我已经将远程验证应用于数量,当我使用一个文本框时,请求必须小于数据库中的可用字段,验证显示,但是当我使用动态表作为列表模型时,验证不会触发。
Public partial class StrRequestDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ReqDetailNo { get; set; }
public int ReqNo { get; set; }
[DisplayName("Category Code")]
[Required(ErrorMessage = "This Category Code Field is required.")]
public int CategoryCode { get; set; }
public int? DirectToPur { get; set; }
[DisplayName("Item Code")]
[Required(ErrorMessage = "This Item Code Field is required.")]
public int ItemCode { get; set; }
public int UnitCode { get; set; }
[Remote(action: "VerifyQuntity", controller: "Store", AdditionalFields = nameof(ItemCode), ErrorMessage = "Quantity must be Less than availability in Store")]
[Required, Range(1, 150, ErrorMessage = "Please enter a value bigger than {1}")]
[DisplayName("Item Quantity")]
public int? ItemQuantity { get; set; }
}
这是我的视图,有循环
@for(int i =0; i< Model.StrRequestDetails.Count; i++)
{
<tr>
<td><select class="form-control" asp-for="@Model.StrRequestDetails[i].CategoryCode" asp-items="@ViewBag.ListofCategory" onchange='Change(this)'>
<option value="">---Select Category---</option>
</select>
<span asp-validation-for="@Model.StrRequestDetails[i].CategoryCode" class="text-danger"> </span> </td>
<td><select class="form-control" asp-for="@Model.StrRequestDetails[i].ItemCode" id="ItemCodes"></select>
<span asp-validation-for="@Model.StrRequestDetails[i].ItemCode" class="text-danger"></span> </td>
<td>
<input class="form-control" asp-for="@Model.StrRequestDetails[i].ItemQuantity" />
<span asp-validation-for="@Model.StrRequestDetails[i].ItemQuantity" class="text-danger"></span> </td>
<td><button type="button" class="btn btn-primary" id="btnremove-@i" onclick="DeleteItem(this);">Delete</button> </td>
</tr>
}
这是我检查数据库插入数量的功能
[AcceptVerbs("get", "post")]
[AllowAnonymous]
public JsonResult VerifyQuntity(int ItemQuantity, int ItemCode)
{
var qutity = _context.StrItemCodes.FirstOrDefault(e => e.ItemCode== ItemCode);
bool result = true;
if(ItemQuantity > qutity.TotalQuaAva)
{
result = false;
}
return Json(result);
}
答: 暂无答案
评论