如何不能够击中控制器在我的asp.net mvc应用程序使用此代码
How to not able to hit the controller in my asp.net mvc application using this code
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
GOTO = function () {
alert("yes");
$.ajax({
cache: false,
type: "POST",
url: "/Home/Index/",
data: datastring,
dataType: "json",
success: function (data) {
alert("Ohh Yaa Success");
}
});
}
</script>
<input type="button" value="submit" onclick="JavaScript:GOTO()" />
</asp:Content>
我的控制器ActionResult是这样的JsonResult
[HttpPost]
public System.Web.Mvc.JsonResult Index(FormCollection collection)
{
//return Content("<xml>this is just test</xml>", "text/xml");
//return Content("this is just test", "text/plain");
if (Request.AcceptTypes.Contains("application/json"))
{
return Json(new { id = 1, value = "new" });
}
else if (Request.AcceptTypes.Contains("application/xml") ||
Request.AcceptTypes.Contains("text/xml"))
{
}
if (Request.AcceptTypes.Contains("text/html"))
{
//return View();
}
return Json(new { foo = "bar", baz = "Blech" });
}
我不能返回json结果在这里总是我得到弹出消息说你已经选择打开这个对话框?我做错了什么吗?
谢谢
试试这个——并确保先加载jQuery。请注意这些变化:通过jQuery而不是内联应用处理程序,序列化数据,动态生成代码中的URL而不是硬编码,并从click处理程序返回false以防止正常的表单提交。
<script type="text/javascript">
$(function() {
$('input[type=button]').click( function() {
var data = $('form').serialize(); // or however you get your data
$.ajax({
cache: false,
type: "POST",
url: "<%= Html.Action( "index", "home" ) %>",
data: data,
dataType: "json",
success: function (data) {
alert("Ohh Yaa Success");
}
});
return false; // don't do the normal submit
});
});
</script>
<input type="button" value="submit" />
您需要将按钮放在表单标签中并在onsubmit事件中调用GOTO函数
看起来您的data: datastring
可能是问题所在。检查并确保data参数的名称与方法参数的名称相同。
我会尝试这样做…
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
$(document).ready(function () {
$(form).submit(function() {
alert("yes");
$.post({
cache: false,
type: "POST",
url: "/Home/Index/",
data: datastring,
dataType: "json",
success: function (data) {
alert("Ohh Yaa Success");
}
});
});
}
</script>
<form>
// your form fields
<input type="button" value="submit" />
</form>
</asp:Content>
然后你的控制器看起来应该是这样的
注意我们如何将参数更改为与jQuery
data
字段匹配的字符串。
[HttpPost]
public System.Web.Mvc.JsonResult Index(string datastring)
{
// you can deserialize your Json here.
//return Content("<xml>this is just test</xml>", "text/xml");
//return Content("this is just test", "text/plain");
if (Request.AcceptTypes.Contains("application/json"))
{
return Json(new { id = 1, value = "new" });
}
else if (Request.AcceptTypes.Contains("application/xml") ||
Request.AcceptTypes.Contains("text/xml"))
{
}
if (Request.AcceptTypes.Contains("text/html"))
{
//return View();
}
return Json(new { foo = "bar", baz = "Blech" });
}
相关文章:
- 在mvc应用程序中,在回发时保留最初隐藏的文本框的隐藏或可见状态
- jQuery在MVC应用程序中设置JS变量
- Ember.js+传单+rails(使用Javascript MVC创建rails应用程序并打开源代码映射)
- 如何将我的Ionic应用程序与我的java服务器(Spring MVC)连接
- 将 MVC 3 应用程序部署到 URL 文件夹已损坏 JavaScript 和内容路径
- 使用peta poco ORM在MVC应用程序中创建选择列表
- 在 MVC 应用程序中组织 JS 代码
- 有没有一种简单的方法可以将ExtJS 4 MVC应用程序转换为ExtJS DeftJS MVC
- 为什么在Firefox更新之后JQuery不能在我的MVC 4应用程序中工作
- Ajax get jsonp给出错误代码500“;请求被阻止”;来自MVC应用程序
- AJAX上重定向视图的正确方式'成功'MVC应用程序中
- 在asp.net mvc应用程序中的iframe中呈现部分视图
- 将数据导出到 MVC 应用程序中的 Excel
- 选项卡在 MVC 应用程序中不起作用
- 单页应用程序 - 我应该在服务器端使用 MVC 框架吗?
- 为什么在此主干 todo-mvc 示例应用程序中需要“应用”
- JavaScript 弹出窗口阻止程序和 Ajax 回调 - C# MVC 应用程序
- JavaScript 在 MVC Web 应用程序中不起作用
- 将值绑定到 Asp.Net MVC 应用程序中的模型
- 如何使用Chatter应用程序MVC显示用户在线