asp.net mvc - How to validate cascading dropdown in MVC -


i developing mvc application , using cascading dropdown in 1 of dropdown. problem need validate both drop downs if user has not selected value. validating both drop downs when clicks on save after immediate opening of pop up, when selects value master drop down(company) , selects value child drop down(customer) , again change value of master drop down child drop down not validating.

here providing code of model, view , controller file.

model-

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 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>();                     } } 

view:

@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");                     (var 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 viewer     disablesavebutton();  </script> 

controller:

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 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 field on sorting needs happen , set                 //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 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 customer comment(s)         /// </summary>         /// <param name="ids">ids of customercomment 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 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>         /// 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());              }         }           /// function used return field used sorting         /// </summary>         /// <param name="sortcol">the column number on sorting needs 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;         }     } } 


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -