Click here to Skip to main content
15,037,928 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends,

i m getting this error since past 2 days. cant get out of it.
i have simple data entry form where u add the name of the client and its information. there are 2 dropdown country and state. on selecting country, state drodpdown is filled. But when i click the save button i get the Error "No Parameter Less constructor Defined for this object"

Here is my code
<pre lang="c#">public class ClientMaster
    {
        
        public int client_ID { get; set; }

        [Required (ErrorMessage="Enter Client Name")]
        public string client_Name { get; set; }

        [Required (ErrorMessage="Enter Client Address")]
        public string client_Address { get; set; }

        public string client_Address1 { get; set; }
        public string client_Address2 { get; set; }

        [Required(ErrorMessage = "Enter Client Phone Number")]
        public long client_Phone { get; set; }
        public long client_Fax { get; set; }
        public long client_Mobile { get; set; }

        
        public int CountryID { get; set; }
        //[Required(ErrorMessage = "Kindly select Country")]
        public string CountryName{ get; set; }

        public int StateID { get; set; }
        //[Required(ErrorMessage = "Kindly Select State")]
        public string StateName { get; set; }

        [Required(ErrorMessage = "Enter the Client City")]
        public string client_City { get; set; }

        [Required(ErrorMessage = "Enter the Client Zip Code")]
        public int client_Zip { get; set; }

        public string last_Updated_By { get; set; }
        public string updated_Date { get; set; }
        public DateTime record_lock_timestamp { get; set; }
        public bool client_status { get; set; }

   

    }



<pre lang="c#">public class ClientMaster_ViewModel
    {
 
        public ClientMaster clsClientMaster { get; set; }
        public SelectList Country { get; set; }
        public SelectList State { get; set; }
        [Required(ErrorMessage="Kindly Select Country")]
        public int selectedCountry { get; set; }
       
    }



HTML
<script type="text/javascript">
    $(document).ready(function () {
        $("#Country").change(function () {
            var c_id = $(this).val();
            $.getJSON("/ClientMasterNew/GetState", { id : c_id }, function (myData) {
                var select = $("#State");
                select.empty();
                $.each(myData, function (index, itemData) {
                    select.append($('<option/>', {
                        value : itemData.ID,
                        text : itemData.StateName
                    }))
               });
            });
        });
    });

</script>
<% using (Html.BeginForm("Create", "ClientMasterNew", FormMethod.Post,this.Model))
   { %>
    <%: Html.ValidationSummary(true, "New Client Creation was unsuccessful. Please correct the errors and try again.")%>
    <fieldset>
        <legend>ClientMaster</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_Name)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_Name)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_Name)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_Address)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_Address)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_Address)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_Address1)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_Address1)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_Address1)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_Address2)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_Address2)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_Address2)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_Phone)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_Phone)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_Phone)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_Fax)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_Fax)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_Fax)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_Mobile)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_Mobile)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_Mobile)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.CountryName)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.selectedCountry,(SelectList)ViewData["Countries"], new { id = "Country" })%>
            <%--new { onchange = "this.form.action='/ClientMasterNew/CreateClient';this.form.submit();" }--%>
            <%--<%: Html.ValidationMessageFor(model => model.clsClientMaster.CountryName)%>--%>
        </div>

         <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.StateName)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.State, (SelectList)ViewData["States"], new { id = "State" })%>
           <%-- <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_State)%>--%>
        </div>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_City)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_City)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_City)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_Zip)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_Zip)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_Zip)%>
        </div>


        <div class="editor-label">
            <%: Html.LabelFor(model => model.clsClientMaster.client_status)%>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.clsClientMaster.client_status)%>
            <%: Html.ValidationMessageFor(model => model.clsClientMaster.client_status)%>
        </div>

        <p>
            <input type="submit" value="Create"  />
        </p>


C#
[AcceptVerbs(HttpVerbs.Get)]
      public JsonResult GetState(string id)
      {
          var StateList = GetStates(Convert.ToInt32(id));

          //Fill_Countries(false, 0, Convert.ToInt32(id));
          return Json(StateList, JsonRequestBehavior.AllowGet);
      }

      public IList<StateMaster> GetStates(int cid)
      {
          //to fill state dropdown
          IList<StateMaster> States = BAL_ClientMaster.Fill_States(cid).ToList();
          return States;
      }

      //[AcceptVerbs(HttpVerbs.Get)]
      //public ActionResult CreateClient(ClientMaster_ViewModel cmVM)
      //{
      //    int countryID = Convert.ToInt32(cmVM.selectedCountry);
      //    Fill_Countries(ref cmVM, true, 0, countryID);
      //    return View(cmVM);
      //}

      //
      // POST: /ClientMasterNew/Create
      [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult Create(FormCollection collection, ClientMaster_ViewModel cmVM)
      {
          try
          {
              int CountryId = 0, stateid = 0;
              // TODO: Add insert logic here
              if (ModelState.IsValid)
              {
                  CountryId = Convert.ToInt32(collection["ClientMaster_ViewModel.Country"]);

                  stateid = Convert.ToInt32(collection["ClientMaster_ViewModel.State"]);

                  //var name = model.client_Name;

                  string[] arr_val = BAL_ClientMaster.InsertClient(cmVM, CountryId, 1, "Ketan Naik");

                  if (Convert.ToBoolean(arr_val[0]) == true)
                  {
                      ViewData["OutPutMsg"] = Convert.ToString(arr_val[1]);
                      return RedirectToAction("Index", "ClientMasterNew");
                  }
                  else
                  {
                      ModelState.AddModelError("", Convert.ToString(arr_val[1]));
                  }
              }
              Fill_Countries(ref cmVM, true, CountryId, stateid);
              return View(cmVM);
          }
          catch
          {
              return View();
          }

          //return View();
      }
Posted
Updated 10-Jul-12 0:37am
v2
Comments
[no name] 10-Jul-12 7:51am
   
The error is very clear. What is it that you do not understand? And, why did you post all of this totally irrelevant code and leave out the important part?

It simply is what it says. Normally, when you don't explicitly define a constructor, a default parameterless constructor is defined. But as soon as you define a constructor with some parameters, the parameterless constructor won't be created. Simply solve it by defining it yourself.
public class Test {

  public Test() {}

  public Test(string test) {
   // ...
  }
}

Good luck!
   
Haven't looked too closely at the code but the chances are something is trying to instantiate your type using some sort of activation (serialisation maybe). If you've got a constructor with a parameter list in it, then there'll be no default constructor - Just add one.

Need to know which type you're talking about and where in code it happens.
   
it was the simple mistake..
i was not returning the View...
its working now!!!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900