Making a Simple Ajax call to controller in Mvc

I´m new to mvc and I try to to do a simple Ajax call to my controller, so I can use a date- and timepickers in my create view.

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

The parameters dictionary contains a null entry for parameter 'Lokal' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult CreateEvent(System.String, System.String, System.String, Int32)' in 'VLVision.Controllers.SammantradesAdminController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameternamn: 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);

}

Since you have different names for the parameter in JS and in C#, it cannot be bound:

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

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

Either change createLokal to lokal in JS or do vice versa in C# (or bind one name to another).


Your parameter name in json is different from the c# action, these two should match. Change one of those:

either change createLokal : lokal to Lokal:lokal or in your action change parameter name to createLokal in your action


Thanks, it did not work at first. But when I debugged it I saw that the result from lokal was a string I change it to a string and then convert it back to a int.

Now I have some other problems, but I think I can fix the rest.

Sry for my crappy English, but I hope you understand what I mean.

Thanks!

链接地址: http://www.djcxy.com/p/6676.html

上一篇: 发送/接收byte []使用AJAX和ASP.NET MVC

下一篇: 在Mvc中对控制器进行简单的Ajax调用