Making a simple Ajax call for a controller in Mvc

I am new to mvc and I am trying to make a simple Ajax call for my controller, so I can use date and timepickers in my creation view.

I get this error message when I use debugging in IE, but if I execute a breakpoint, it looks like I got the correct data.

The parameter dictionary contains a null entry for the parameter "Lokal" of a non-zero type, "System.Int32" for the method 'System.Web.Mvc.ActionResult CreateEvent (System.String, System.String, System.String, Int32)' in ' VLVision.Controllers.SammantradesAdminController. The optional parameter must be a reference type, a null type, or declared as an optional parameter. Parameter: Parameters

HTML                    

script type="text/javascript">

        function createSammantrade() {
        var sammantrade = document.getElementById('sammantrade').value;
        var date = document.getElementById('datepicker').value;
        var startTime = date + ' ' + document.getElementById('StartTimepicker').value;
        var endTime = date + ' ' + document.getElementById('EndTimepicker').value;
        var lokal = document.getElementById('lokal').value;

        $.ajax({
            url: "@Url.Action("CreateEvent", "SammantradesAdmin")",
            data: { createSammantrade: sammantrade, createStartTime: startTime, createEndTime: endTime, createLokal: lokal },
            type: "POST",
            error: function () {
                alert("An error occurred.");
            },
            success: function (data) {

                $("#clanderDiv").html(data);
                $("#setEventResponse").html("HΓ€ndelse sparad");
            //    $(".blank").tooltip();
            }
        });
    }

controller

public ActionResult Create()
{
       ViewBag.lID = new SelectList(db.Lokal, "lID", "lLokal");
       return View();
}

[HttpPost]
public ActionResult CreateEvent(string createSammantrade, string createStartTime, string createEndTime, int Lokal)
        {
            Sammantrade sammantrade = new Sammantrade();
            sammantrade.sSammantrade = createSammantrade;
            sammantrade.sStartTid = Convert.ToDateTime(createStartTime);
            sammantrade.sSlutTid = Convert.ToDateTime(createEndTime);
            sammantrade.lID = Lokal;


            if (ModelState.IsValid)
            {
                db.Sammantrade.Add(sammantrade);
                db.SaveChanges();
                return RedirectToAction("Index");

            }
                ViewBag.lID = new SelectList(db.Lokal, "lID", "lLokal", sammantrade.lID);
                return View(sammantrade);

}
+4
3

JS #, :

data: { createSammantrade: sammantrade, createStartTime: startTime, createEndTime: endTime, createLokal: lokal }

public ActionResult CreateEvent(string createSammantrade, string createStartTime, string createEndTime, int Lokal)

createLokal lokal JS, # ( ).

+3

json #, . :

createLokal : lokal Lokal:lokal, createLokal

+2

, . , , lokal , , int.

, , .

, , , .

!

0
source

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


All Articles