Getting the error "Without parameters without constructor", do not know why

For some reason, one of my own AJAX calls gets the "No parameters without constructor" error. Here is the code:

CallAndReplace(JSON.stringify(model), url, $("#panel")); function CallAndReplace(data, url, replace) { $.ajax({ url: url, type: "post", contentType: "application/json; charset=utf-8", data: data, success: function (result) { replace.html(result); }, error: function (x, e) { if (x.status == 0) { alert('You are offline!!\n Please Check Your Network.'); } else if (x.status == 404) { alert('Requested URL not found.'); } else if (x.status == 500) { alert('Internal Server Error.'); } else if (e == 'parsererror') { alert('Error.\nParsing JSON Request failed.'); } else if (e == 'timeout') { alert('Request Time out.'); } else { alert('Unknow Error.\n' + x.responseText); } } }); } 

'model' is the viewmodel in my MVC-3 view, which I converted to a Javascript object. 'url' is a url created using the @@ Url.Action method ("Action", "Controller"). And $ ("# panel") is the area of ​​the div, which is replaced by the partial view returned by the controller action.

When I try to debug a project, it never gets into the action of the controller. When I created a dummy controller action without parameters, it gets there in debug mode. But I am obviously sending data. I can see that the data is published in Firebug (although for some reason it is not structured), but apparently it is not sent, and I do not know why.

I use CallAndReplace 20 in my code for other purposes and have never asked me this problem. I totally don’t understand why.

Edit: Here is the viewmodel class that I submit to the view:

 public class AwardsEdit { public List<AwardsViewModel> Awards { get; set; } public int TitleId { get; set; } public List<Tuple<int, string>> Participants { get; set; } public List<Award1> AllAwards { get; set; } public List<Tuple<int, string>> AllAwardCompanies { get; set; } } 

And the controller action I'm trying to call:

 public PartialViewResult SaveAwards(AwardsEdit award) { if (ModelState.IsValid) { bool updated = _translator.UpdateAward(award); if (updated) { return PartialView("Details", _translator.GetAwards(award.TitleId)); } //else error ModelState.AddModelError("", "Award data was not saved."); } //on error, load meta data var data = _translator.GetAwards(award.TitleId, true); award.Participants = data.Participants; award.AllAwards = data.AllAwards; award.AllAwardCompanies = data.AllAwardCompanies; return ViewAwards(award.TitleId); } 

The controller itself does not have a constructor method without parameters, and I use dependency injection, but I have other AJAX calls that cause various actions in this controller, and they work fine. I do not know why this one does not work.

+6
source share
7 answers

The error probably refers to the type of one of your parameter actions (and not to the controller, as others suggested).
MVC cannot populate a parameter if it cannot be constructed first.

For example, you may get the same error as this:

 public class ParameterConstructor { public ParameterConstructor(string parameter){ } } public class MyController : Controller { public ActionResult Test(ParameterConstructor model) { return "This action will not be reached"; } } 

So, you need to make sure that your model type has a constructor without parameters.

Update

In response to your updated code, it is true that your ViewModel constructor has no parameters.
However, you have a list of Tuple<int, string> . The documentation states that Tuple does not have a constructor without parameters. What a problem - Tuple is for reading only. Perhaps you could use KeyValuePair<> instead?

+10
source

If you want to pass JSON to your server, you must pass it as a parameter value. Just setting the "data" property in an AJAX call will not work.

You can use:

  data: { theJson: data }, 

then your server will see the "theJSON" parameter and can decode the value of this to return the data structure.

0
source

.stringify is the reason it is not structured. This call will take your data, add [forward, a] to the back, and then create a comma, the designated line of data that you sent. Perhaps your controller expects an object or model and receives a string instead.

0
source

I would make sure that the controller referenced by your @Url.Action("Action", "Controller") has a constructor without parameters. Or it may happen that you do not expect a constructor without parameters, in which case you must make sure that your IoC container is correctly configured to enter the necessary dependency.

It can also be the ViewModel itself, in which case you must make sure that the constructor without parameters (by default) exists in the ViewModel type.

Example:

 public class Example { // this is a parameterless constructor public Example() { // ... } } 
0
source
 public class MyController : Controller { // make sure constructor has default parameterless constructor public MyController() {} } 
0
source

This error message is not the biggest - it sends people to one of the most probable reasons for the controller class not created. But he really should read: "The controller design and initialization failed. One of the reasons is that there is no constructor without parameters, but you might have screwed it in another way. There was an internal exception ..."

The best bet for its trap would be to run your code in debug mode, Visual Studio will throw you an exception when this happens.

0
source

Since you are using Dependency Injection, you need to dig into the code to find out why the dependency injectors do not instantiate the controller with the required parameters.

0
source

Source: https://habr.com/ru/post/903714/


All Articles