Parameter error when saving data

-2

I can not save some data, probably the error is in the parameters.

My Controller:

 public ActionResult SalvarMilestone(Milestone milestone, int CodigoProjeto)
    {
        //if (ModelState.IsValid)
        //{
        try
        {
            using (CPMDatabaseEntities dc = new CPMDatabaseEntities())
            {
                milestone.CodigoProjeto = CodigoProjeto;
                dc.Milestone.Add(milestone);
                dc.SaveChanges();
            }
        }
        catch (Exception)
        {

            throw;
        }
        //}

        return View(milestone);
    }

My SCRIPT that returns the data:

<script>
$(document).ready(function () {       
   $("#AjaxPost").click(function () {
        var dataObject = {
            Descricao: $("#Descricao").val(),
            codigoProjeto: $("#CodigoProjeto").val(),
            TipoCalculo: $('#TipoCalculo :selected').text(),
        };

        $.ajax({
            url: "@Url.Action("SalvarMilestone", "Dashboard")",
            type: "POST", 
            data: dataObject,
            dataType: "json"
        });
    });
});

Mod SCRIPT:

  $(".AddMarco").click(function () {
    var CodigoProjeto = $(this).attr("data-id");
    $("#modal").load("AddMilestone?CodigoProjeto=" + CodigoProjeto, function () {
        $(".modal").modal();
    })
});

I get the value of the project code with this button

 <input id="AjaxPost" value="Salvar" data-id="@ViewBag.CodigoProjeto" class="btn btn-primary" data-dismiss="modal" />

Error you are giving:

The parameters dictionary contains a null entry for parameter 'CodigoProjeto' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult SalvarMilestone(ISystemCritical.Models.Milestone, Int32)' in 'ISystemCritical.Controllers.DashboardController'. 
An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 
Nome do parâmetro: parameters

Probably the error is in the parameter passing, because when I access the Dashboard / SaveMilestone link? ProjectCode = 1 it runs my controller.

    
asked by anonymous 20.10.2015 / 02:04

1 answer

0

Ensure that you wrote down [HttpPost] before Action to accept the POST method for Ajax:

[HttpPost]
public ActionResult SalvarMilestone(Milestone milestone, int CodigoProjeto)
{ 
    ...

EDIT

ASP.NET MVC Binder case-sensitive , then this:

codigoProjeto: $("#CodigoProjeto").val(),

Should be replaced by:

CodigoProjeto: $("#CodigoProjeto").val(),
    
21.10.2015 / 00:32