0

I'm working at an ASP.NET MVC website using C#.

On a page I try to send back a complete model to the controller. In this model most inputs are unique variables, and there is an array.

In the model, the array is defined like this:

public class SimulazioneModelComplete
...
public SoaValueModel[] GareSoaSec { get; set; }

The razor / html code (very simplified!) is like this

@model SimulazioneModelComplete

@using (Html.BeginForm("CreaEsito", "Simulazioni", FormMethod.Post, new { id = "CreaEsito", name = "CreaEsito", enctype = "multipart/form-data" }))
{
    ...
    @foreach (SoaModel soa in ViewBag.Categorie)
    <input type="checkbox" name="GareSoaSec[@counter].IdSoa" id="GareSoaSec[@counter].IdSoa" value="@soa.id" />
    ...
}

The user posts data clicking a button:

<button onclick="Save()">SAVE</button>

The onclick javascript is like this:

function Save() 
{
   $.post("/Abbonamenti/Simulazioni/CreaEsito", $("CreaEsito").serialize(), function (data) { });
};

The CreaEsito method is like this:

 [HttpPost]
 [AuthorizedOnly(Roles = "Administrator, Agent, Esiti")]
 public ActionResult CreaEsito(SimulazioneModelComplete model, SoaValueModel[] GareSoaSec) 

I try in a lot of ways, but I never have a complete model, with both the simple variables and the array of objects. It seems like the "link" made by "name" doesn't work properly, and I don't understand why.

It should work well (I do the same in other pages, and everything works well), but this time it doesn't work. When I look the model sent back to the controller, the unique variables are ok, the array is empty, length is zero.

I've lost all Friday and Saturday, now is 10PM and I'm still fighting, without any solution.

Can someone help?

Giorgio Forti
  • 131
  • 2
  • 14
  • Try using $.ajax, see below example by Halcyon. https://stackoverflow.com/questions/13242414/passing-a-list-of-objects-into-an-mvc-controller-method-using-jquery-ajax – Thomson Mixab Jul 17 '21 at 21:33
  • I'll try this way, but this isn't the solution I'm searching for: I know for sure MVC can send a complete "model" to the controller. I've done the same thing in other pages, this time the same methon doesn't work. – Giorgio Forti Jul 19 '21 at 12:55
  • @Giorgio Forti: Add your action method `CreaEsito` declaration to the post, please. – Jackdaw Jul 19 '21 at 13:04
  • @Jackdaw: I edited and added the method. – Giorgio Forti Jul 20 '21 at 14:57

0 Answers0