我有一个MVC3的操作方法有3个参数是这样的:调用MVC操作方法,通过JavaScript,但不使用AJAX
var url = "/Question/Insert?" + "_strTitle='" + title + "'&_strContent='" + content + "'&_listTags='" + listTags.toString() + "'";
,我想通过正常的JavaScript函数不是AJAX调用这个(因为它是没有必要使用AJAX功能) 我试图用这个功能,但它没有工作:
window.location.assign(url);
它并没有跳到插入QuestionController的作用。
有人想帮我吗?非常感谢
这是更详细
我要插入新问题的数据库,但我必须从CKEDITOR获取数据,所以我必须使用下面这个函数来获取和验证数据
// insert new question
$("#btnDangCauHoi").click(function() {
//validate input data
//chủ đề câu hỏi
var title = $("#txtTitle").val();
if (title == "") {
alert("bạn chưa nhập chủ đề câu hỏi");
return;
}
//nội dung câu hỏi
var content = GetContents();
content = "xyz";
if (content == "") {
alert("bạn chưa nhập nội dung câu hỏi");
return;
}
//danh sách Tag
var listTags = new Array();
var Tags = $("#list_tag").children();
if (Tags.length == 0) {
alert("bạn chưa chọn tag cho câu hỏi");
return;
}
for (var i = 0; i < Tags.length; i++) {
var id = Tags[i].id;
listTags[i] = id;
//var e = listTags[i];
}
var data = {
"_strTitle": title,
"_strContent": content,
"_listTags": listTags.toString()
};
// $.post(url, data, function (result) {
// alert(result);
// });
var url = "/Question/Insert?" + "_strTitle='" + title + "'&_strContent='" + content + "'&_listTags='" + listTags.toString() + "'";
window.location.assign(url); // I try to use this, and window.location also but they're not working
});
此网址拨打MVC行动由POST方法
[HttpPost]
[ValidateInput(false)]
public ActionResult Insert(string _strTitle, string _strContent, string _listTags)
{
try
{
//some code here
}
catch(Exception ex)
{
//if some error come up
ViewBag.Message = ex.Message;
return View("Error");
}
// if insert new question success
return RedirectToAction("Index","Question");
}
“插入” 下面如果插入成功的行动,它将[R直接转到索引页面列出所有包含新问题的问题已插入。如果没有,它会显示错误页面。所以,这就是我不使用AJAX的原因
有没有人帮助我?谢谢:)
为什么你不想使用AJAX? – jrummell
您是否收到错误或页面是否无法导航?你的控制器是一个POST还是GET? – Terry
也许你可以给我们你的路线和行动方法的语法? window.location应该工作,除非你的路由不正确。你有什么错误吗? – Ronald