我正在开发一个 MVC 应用程序,并且在我的一个下拉菜单中使用 Cascading 下拉菜单。问题是如果用户没有选择任何值,我需要验证两个下拉菜单。目前,当您在立即打开弹出窗口后单击保存时,它会验证两个下拉菜单,但是当您从主下拉菜单(公司)中选择值并从子下拉菜单(客户)中选择值并再次更改主下拉菜单的值时下拉不验证。
在这里,我提供了我的模型、视图和控制器文件的代码。
模型-
public class CustomerComment : BaseModel
{
[Required(ErrorMessage = "Please select Customer name")]
public int InvoiceCustomerId { get; set; }
public string OldCustomerId { get; set; }
[Display(Name = "Invoice Customer")]
public string InvoiceCustomerName { get; set; }
[Display(Name = "Invoice Customer")]
public string InvoiceCustomer { get; set; }
[Required(ErrorMessage = "Please select Company name")]
public int CompanyId { get; set; }
[Display(Name = "Company Name")]
[Required(ErrorMessage = "Please enter Company name")]
public string CompanyName { get; set; }
public string Company { get; set; }
[Display(Name = " Comment")]
[StringLength(220, ErrorMessage = "Customer comment must be maximum length of 220")]
public string Comment { get; set; }
[Display(Name = "Group")]
public bool Group { get; set; }
public List<InvoiceCustomer> InvoiceCustomerList { get; set; }
public CustomerComment()
{
InvoiceCustomerList = new List<InvoiceCustomer>();
}
}
看法:
@model ACS.MDB.Net.App.Models.CustomerComment
@{
ViewBag.Title = "MDB::Customer Comment";
}
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<div style="background: #F9F7FB">
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
//hidden field
@Html.HiddenFor(m => m.ID)
@Html.HiddenFor(m => m.InvoiceCustomerId, new { @id = "txtCustomerId" })
@Html.HiddenFor(m => m.CompanyId, new { @id = "txtCompanyId" })
@Html.HiddenFor(m => m.InvoiceCustomerName, new { @id = "txtCustomerName" })
@Html.HiddenFor(m => m.CompanyName, new { @id = "txtCompanyName" })
<table style="width: 100%">
<tr>
<td></td>
</tr>
@if (Model.ID != 0)
{
<tr>
<td>
@Html.LabelFor(m => m.CompanyName)
</td>
<td>
<b>@Html.DisplayFor(m => m.CompanyName, new { style = "width:300px" })</b>
</td>
</tr>
<tr><td></td></tr>
<tr><td></td></tr>
}
else
{
<tr>
<td>
@Html.LabelFor(m => m.CompanyName, new { @class = "mandatoryLabel" })
</td>
<td>
@Html.DropDownListFor(x => x.CompanyId, new SelectList(Model.OAcompanyList, "ID", "Name", Model.CompanyId), "Select Company", new { @id = "ddlCompany", style = "width:300px;height:25px" })
</td>
</tr>
<tr>
<td></td>
<td>
@Html.ValidationMessageFor(model => model.CompanyId)
</td>
</tr>
}
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
@if (Model.ID != 0)
{
<tr>
<td>
@Html.LabelFor(m => m.InvoiceCustomerName)
</td>
<td>
<b>@Html.DisplayFor(m => m.InvoiceCustomerName, new { style = "width:300px" })</b>
</td>
</tr>
<tr><td></td></tr>
<tr><td></td></tr>
}
else
{
<tr>
<td>
@Html.LabelFor(model => model.InvoiceCustomerName, new { @class = "mandatoryLabel" })
</td>
<td>
@Html.DropDownListFor(x => x.InvoiceCustomerId, new SelectList(Model.InvoiceCustomerList, "ID", "CustomerandOACustomerId", Model.InvoiceCustomerId), "Select Customer", new { @id = "ddlInvCustomer", style = "width:300px;height:25px" })
</td>
</tr>
<tr>
<td></td>
<td>
@Html.ValidationMessageFor(m => m.InvoiceCustomerId)
</td>
</tr>
}
<tr><td></td></tr>
<tr>
<td style="vertical-align:middle">
@Html.LabelFor(m => m.Comment)
</td>
<td>
@Html.TextAreaFor(m => m.Comment, 4, 1, new { @id = "txtCustomerComment", @maxlength = "220", style = "width:290px; resize: none" })
</td>
</tr>
<tr>
<td></td>
<td>
@Html.ValidationMessageFor(model => model.Comment)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(m => m.Group, new { style = "width:200px" })
</td>
<td>
@Html.CheckBoxFor(m => m.Group)
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
}
</div>
<script type="text/javascript">
SetFocus('#ddlCompany');
DisableInvalidKeyForControl('#txtCustomerComment');
if ('@Model.ID' > 0)
{
SetFocus('#txtCustomerComment');
}
var selectedCustomerId = -1;
var selectedCompanyId = -1;
var selectedCustomerName = '';
var selectedCompanyName = '';
$("#ddlCompany").change(function () {
//var selectedCustomerId = -1;
selectedCompanyId = $(this).val();;
selectedCompanyName = $(this).children('option').filter(':selected').text();
$("#txtCompanyId").val(selectedCompanyId);
$("#txtCompanyName").val(selectedCompanyName);
if (selectedCompanyId != -1 && selectedCompanyId != 0) {
var postData = { companyId: selectedCompanyId };
//Get InvoiceCustomerList based on selected company id
$.post("/Contract/GetInvoiceCustomerListByCompany", postData, function (response) {
if (response != null) {
$("#ddlInvCustomer").empty();
$("#ddlInvCustomer").length = -1;
AddOption("Select Customer", "-1", "#ddlInvCustomer");
for (var i in response) {
AddOption(response[i].CustomerandOACustomerId, response[i].InvoiceCustomerId, "#ddlInvCustomer");
}
}
});
}
else {
$("#ddlInvCustomer").empty();
selectedCompanyId = 0;
selectedCustomerId = 0;
$("#ddlInvCustomer").val = 0;
AddOption("Select Customer", "0", "#ddlInvCustomer");
}
})
$("#ddlInvCustomer").change(function () {
selectedCustomerId = $(this).val();
selectedCustomerName = $(this).children('option').filter(':selected').text();
if (selectedCustomerId != -1 && selectedCustomerId != 0) {
$("#txtCustomerId").val(selectedCustomerId);
$("#txtCustomerName").val(selectedCustomerName);
}
//else {
// $("#ddlInvCustomer").empty();
// selectedCustomerId = 0;
// $("#ddlInvCustomer").val = 0;
// AddOption("Select Customer", "0");
//}
})
//Disable Save button of pop up for viewer
DisableSaveButton();
</script>
控制器:
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ACS.MDB.Net.App.Common;
using ACS.MDB.Net.App.Models;
using ACS.MDB.Net.App.Services;
using ACS.MDB.Net.App.ValueObjects;
using MODEL = ACS.MDB.Net.App.Models;
namespace ACS.MDB.Net.App.Controllers
{
public partial class ContractController
{
/// <summary>
/// Returns CustomerComment index view
/// </summary>
/// <returns>CustomerComment index view</returns>
/// GET: /ContractController/CustomerComment/
public ActionResult CustomerCommentIndex()
{
return IndexView();
}
/// <summary>
/// Gets the customer comment list
/// </summary>
/// <param name="param"></param>
/// <returns>Customer comment List</returns>
public ActionResult CustomerCommentList(MODEL.jQueryDataTableParamModel param)
{
try
{
List<Company> companyList = Session.GetUserAssociatedCompanyList();
List<CompanyVO> companyVOList = new List<CompanyVO>();
foreach (var item in companyList)
{
companyVOList.Add(new CompanyVO(item));
}
CustomerCommentService customerCommentService = new CustomerCommentService();
List<MODEL.CustomerComment> customerCommentList = new List<CustomerComment>();
List<CustomerCommentVO> customerCommentVOList = customerCommentService.GetCustomerCommentList(companyVOList);
foreach (var item in customerCommentVOList)
{
customerCommentList.Add(new MODEL.CustomerComment(item));
}
//get the field on with sorting needs to happen and set the
//ordering function/delegate accordingly.
int sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
var orderingFunction = GetCustomerCommentOrderingFunction(sortColumnIndex);
var result = GetFilteredObjects(param, customerCommentList, orderingFunction);
return result;
}
catch (Exception e)
{
return new HttpStatusCodeAndErrorResult(500, e.Message);
}
}
/// <summary>
/// Create new customer comment
/// </summary>
/// <returns>The Customer comment details view</returns>
public ActionResult CustomerCommentCreate()
{
try
{
MODEL.CustomerComment customerComment = new MODEL.CustomerComment();
customerComment.OAcompanyList = Session.GetUserAssociatedCompanyList();
return PartialView("CustomerCommentDetails", customerComment);
}
catch (Exception e)
{
return new HttpStatusCodeAndErrorResult(500, e.Message);
}
}
/// <summary>
/// Edit customer comment
/// </summary>
/// <param name="id">The customer comment id</param>
/// <returns>Customer comment details</returns>
public ActionResult CustomerCommentEdit(int id)
{
MODEL.CustomerComment customercomment = new CustomerComment();
try
{
CustomerCommentService customerCommentService = new CustomerCommentService();
//Get Customercomment details
CustomerCommentVO customerCommentVO = customerCommentService.GetCustomerCommentById(id);
if (customerCommentVO == null)
{
ModelState.AddModelError("", String.Format(Constants.ITEM_NOT_FOUND, Constants.CUSTOMERCOMMENT));
}
else
{
customercomment = new CustomerComment(customerCommentVO);
}
}
catch (Exception e)
{
ModelState.AddModelError("", e.Message);
}
return PartialView("CustomerCommentDetails", customercomment);
}
/// <summary>
/// Save the customer comment
/// </summary>
/// <param name="model">model object</param>
/// <returns></returns>
public ActionResult CustomerCommentSave(MODEL.CustomerComment model)
{
try
{
if (model.InvoiceCustomerId == 0)
{
throw new ApplicationException("Please select Customer name");
}
else
{
CustomerCommentService customerCommentService = new CustomerCommentService();
if (ModelState.IsValid)
{
//Get user id
int? userId = Session.GetUserId();
if (userId.HasValue)
{
CustomerCommentVO customerCommentVO = new CustomerCommentVO(model, userId);
customerCommentService.SaveCustomerComment(customerCommentVO);
}
return new HttpStatusCodeResult(200);
}
else
{
throw new ApplicationException(String.Format(Constants.CANNOT_SAVE, Constants.CUSTOMERCOMMENT));
}
}
}
catch (ApplicationException e)
{
return new HttpStatusCodeAndErrorResult(500, e.Message);
}
}
/// <summary>
/// Delete the Customer comment(s)
/// </summary>
/// <param name="Ids">Ids of customerComment to be deleted</param>
public ActionResult CustomerCommentDelete(List<int> ids)
{
try
{
CustomerCommentService customerCommentService = new CustomerCommentService();
customerCommentService.DeleteCustomerComment(ids);
return new HttpStatusCodeResult(200);
}
catch (Exception e)
{
return new HttpStatusCodeAndErrorResult(500, e.Message);
}
}
/////// <summary>
/////// Gets the Customer List
/////// </summary>
/////// <returns>Invoice Customer List</returns>
//public List<MODEL.InvoiceCustomer> GetCustomerList()
//{
// MODEL.CustomerComment customerComment = new CustomerComment();
// InvoiceCustomerService invoiceCustomerService = new InvoiceCustomerService();
// List<InvoiceCustomerVO> invoiceCustomerVOList = invoiceCustomerService.GetAllCustomerList();
// foreach (InvoiceCustomerVO item in invoiceCustomerVOList)
// {
// customerComment.InvoiceCustomerList.Add(new MODEL.InvoiceCustomer(item));
// }
// return customerComment.InvoiceCustomerList;
//}
///// <summary>
///// Gets invoice customer list based on customer name
///// </summary>
///// <param name="customerName">customer name</param>
///// <returns>Invoice customer List</returns>
//public JsonResult GetCompanyByCustomerName(string customerName)
//{
// try
// {
// InvoiceCustomerService invoiceCustomerService = new InvoiceCustomerService();
// List<CompanyVO> companyVOList = invoiceCustomerService.GetCompanyListByCustomerName(customerName);
// //List<InvoiceCustomerVO> invoiceCustomerVOList = invoiceCustomerService.GetCompanyByCustomerName(customerName);
// return Json(companyVOList);
// }
// catch (Exception e)
// {
// return Json(new ApplicationException());
// }
//}
/// <summary>
/// Get InvoiceCustomer List based on companyId
/// </summary>
/// <param name="companyId">company Id</param>
/// <returns>Invoice customer List based on company Id</returns>
public JsonResult GetInvoiceCustomerListByCompany(int companyId)
{
try
{
InvoiceCustomerService invoiceCustomerService = new InvoiceCustomerService();
List<InvoiceCustomerVO> invoiceCustomerVOList = invoiceCustomerService.GetInvoiceCustomerList(companyId);
return Json(invoiceCustomerVOList);
}
catch (Exception e)
{
return Json(new ApplicationException());
}
}
/// The function used to return field used for sorting
/// </summary>
/// <param name="sortCol">The column number on which sorting needs to happen</param>
/// <returns></returns>
public Func<BaseModel, object> GetCustomerCommentOrderingFunction(int sortCol)
{
Func<BaseModel, object> sortFunction = null;
switch (sortCol)
{
case 2:
sortFunction = obj => ((MODEL.CustomerComment)obj).InvoiceCustomer;
break;
case 3:
sortFunction = obj => ((MODEL.CustomerComment)obj).Company;
break;
case 4:
sortFunction = obj => ((MODEL.CustomerComment)obj).Comment;
break;
default:
sortFunction = obj => ((MODEL.CustomerComment)obj).ID;
break;
}
return sortFunction;
}
}
}