制作一个简单的Ajax调用在MVC中到控制器 [英] Making a Simple Ajax call to controller in Mvc
问题描述
I'm新MVC和我尝试做一个简单的Ajax调用我的控制器,这样我就可以用我创建一个视图日期戳和timepickers。
我收到此错误信息,当我在IE中使用调试,但如果我做一个断点,它看起来像我得到正确的数据。
参数字典包含参数'LOKAL非可空类型System.Int32的的方法无效项 System.Web.Mvc.ActionResult CreateEvent的(System.String,System.String, System.String,的Int32)在 VLVision.Controllers.SammantradesAdminController。可选 参数必须是引用类型,可空类型,或声明为 。可选参数
Parameternamn:参数的
HTML
脚本类型=文/ JavaScript的>
功能createSammantrade(){
。VAR sammantrade =的document.getElementById('sammantrade')值;
。VAR日期=的document.getElementById('日期选择器)的价值;
VAR的startTime =日期+''+的document.getElementById('StartTimepicker')值。
VAR endTime的=日期+''+的document.getElementById('EndTimepicker')值。
VAR LOKAL =的document.getElementById('LOKAL')值。
$阿贾克斯({
网址:@ Url.Action(CreateEvent的,SammantradesAdmin),
数据:{createSammantrade:sammantrade,createStartTime:startTime时,createEndTime要求:结束时间,createLokal:LOKAL},
键入:POST,
错误:函数(){
警报(发生错误。);
},
成功:功能(数据){
$(#clanderDiv)的HTML(数据)。
$(#setEventResponse)HTML(Händelsesparad)。
// $(空白。)提示()。
}
});
}
控制器
公众的ActionResult创建()
{
ViewBag.lID =新的SelectList(db.Lokal,锅盖,lLokal);
返回查看();
}
[HttpPost]
公众的ActionResult CreateEvent的(字符串createSammantrade,串createStartTime,串createEndTime,INT LOKAL)
{
Sammantrade sammantrade =新Sammantrade();
sammantrade.sSammantrade = createSammantrade;
sammantrade.sStartTid = Convert.ToDateTime(createStartTime);
sammantrade.sSlutTid = Convert.ToDateTime(createEndTime);
sammantrade.lID = LOKAL;
如果(ModelState.IsValid)
{
db.Sammantrade.Add(sammantrade);
db.SaveChanges();
返回RedirectToAction(指数);
}
ViewBag.lID =新的SelectList(db.Lokal,锅盖,lLokal,sammantrade.lID);
返回查看(sammantrade);
}
既然你有不同的名称为JS和C#中的参数,它不能被绑定:
数据:{createSammantrade:sammantrade,createStartTime:startTime时,createEndTime要求:结束时间,createLokal:LOKAL}
公众的ActionResult CreateEvent的(字符串createSammantrade,串createStartTime,串createEndTime,INT LOKAL)
要么改变 createLokal
到 LOKAL
在JS或者做相反在C#(或绑定一个名称到另一个)
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).
这篇关于制作一个简单的Ajax调用在MVC中到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!