I have a jQuery script that captures a form into an object and is supposed to send that object to a controller method. Here's the script:
var modalConfirm = function (callback) {
$("#modal-btn-si").on("click", function () {
    callback(true);
    $("#modal-confirm").modal('hide');
});
$("#modal-btn-no").on("click", function () {
    callback(false);
    $("#modal-confirm").modal('hide');
});};
function confirmar(form, text) {
$("#modal-confirm").modal('show');
modalConfirm(function (confirm) {
    if (confirm) {
        enviar(form);
    }
}); };
function enviar(form) {
debugger;
var datos = $('#' + form).serializeArray(),
    dataObj = {};
$(datos).each(function (i, field) {
    dataObj[field.name] = field.value;
});
if (typeof dataObj["precio"] === "undefined") { dataObj["precio"] = 0; }
$.post("NuevaOpcion", {
    contentType: "application/JSON",
    data: JSON.stringify(dataObj),
}); }
Note: the script was originally much simpler, but trying to fix the issue I'm describing forced me to divide the code in the functions you see.
I created a model to receive the form's data:
public class NuevaOpcionFormModel {
    public NuevaOpcionFormModel() { }
    public int Id { get; set; }
    public string nombre { get; set; }
    public int cantidad { get; set; }
    public int precio { get; set; }
    public int idrespuesta { get; set; }
    public int OptionSelectedItem { get; set; }
}
And the controller's method signature:
[HttpPost]
    public ActionResult NuevaOpcion (Dayvo.Presupuestador.Web.Models.Form.NuevaOpcionFormModel nuevaOpcion) { ... }
What I've tried
On the script:
- .serialize()instead of- .serializeArray().
- Not serialize at all.
- Passing entire 'form' into object and then to controller.
- JSON.stringify(actual state) and not using it.
- Stablishing each field in datamanually.
- Stablishing each field in datamanually and apllyingJSON.stringify.
On the controller:
- [FromForm]&- [FromBody]
- [Bind]
- A single variable for each field, matching names and types.
Everything I've tried ends up with the controller's method receiving nothing. I tracked the script (that's why you're seeing a debugger; breakpoint in it), it gets the object into the array correctly, each field with it's proper value:
But still hits the controller with empty object:
Any clue about what I'm doing wrong is more than welcome. Thanks in advance.
EDIT
As requested, here's the view part I'm capturing into the form:
<div class="panel-footer">
            @using (Html.BeginForm("NuevaOpcion", "Home", FormMethod.Post, new { @id = "frm_nueva_opcion" })) {
                @Html.HiddenFor(m => m.Id)
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group" style="margin-bottom: .7em;margin-top: .7em;">
                            <button class="btn btn-success btn-xs" type="button" onclick=" $('#row-nueva-opcion').toggle()" id="add-opcion">
                                <span class="glyphicon glyphicon-plus-sign"></span> Añadir nueva opción
                            </button>
                        </div>
                    </div>
                </div>
                <div class="row" id="row-nueva-opcion" style="display:none">
                    <div class="col-md-10">
                        <label>
                            <input type="checkbox" id="opcion-extra" onclick=" $('#nuevo-precio').attr('disabled', !this.checked);" />
                            Es opción extra
                        </label>
                        <div class="input-group" style="margin-bottom:1.7em;">
                            <input type="text" placeholder="Opción" class="form-control" name="nombre" style="max-width:70%;">
                            <input type="number" placeholder="Cantidad" min="1" value="1" class="form-control" name="cantidad" style="max-width:15%;">
                            <input type="number" placeholder="Precio" class="form-control" id="nuevo-precio" name="precio" style="max-width:15%;" disabled>
                            <input type="hidden" name="idrespuesta"  id="idrespuesta" value="@listItems.Select(x=>x.Value).FirstOrDefault()" />
                            <div class="input-group-addon">€</div>
                            <span class="input-group-btn">
                                <a class="btn btn-primary" data-title="Confirmación de acción" data-toggle="modal" data-target="#modal_confirm" onclick="confirmar('frm_nueva_opcion')">
                                    <span class="glyphicon glyphicon-floppy-disk"></span> Guardar
                                </a>
                            </span>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="form-group">
                            <label>
                                ¿Para que pregunta es la opción?
                                @Html.DropDownList("OptionSelectedItem", listItems, new { @class = "form-control" })
                            </label>
                        </div>
                    </div>
                </div>
            }
        </div>



.serialize(), not.serializeArray(), and you need to removecontentType: "application/JSON",and use$.post("NuevaOpcion", datos)(wheredatosis$('#' + form).serialize();). And that assumes the name attributes of your form match the names of the properties (you need to show your view)