ASP.NET MVC两个用户控制 [英] ASP.NET MVC two user control
问题描述
我在页面上和两个用户控制用户控制的一个有这段文字的aread。
这是用来添加注释和,但是当他们点击添加备注按钮的页面重新加载。
我不想重新加载页面,我一直在寻找这这是一个没有完成的一个例子
回发。
感谢
我厌倦了这样使用JSON,但它引发以下错误
用于访问路径的HTTP动词POST/文件/ TestNote /文件/ AddNote'是不允许的。
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){ $(#btnAddNote)。点击(函数(){
警报(磕磕);
变种的Gnote = getNotes();
//变种注意到= $(#txtNote)VAL();
如果(Gnote的== NULL){
警报(注意为空);
返回; } $。员额(文档/ AddNote,Gnote的,功能(数据){
VAR味精= data.Msg;
$(#resultMsg中)HTML(MSG)。
});
});
}); 功能getNotes(){
警报(我在getNotes功能);
。VAR票据= $(#txtNote)VAL();
如果(注==)
警报(票据为空);
返回(注==)?空:{注:注};
}
< / SCRIPT>
< / ASP:内容>
像这样的东西会给你将数据发送到一个动作的能力做一些逻辑,并返回一个JSON的结果,那么就可以相应地更新你的视图。
JavaScript函数
函数AddNote(){
变种D =新的日期(); // IE劈死prevent缓存 $ .getJSON('/ myController的/ MyAction',{数据:世界你好,日期:d.getTime()},功能(数据){
警报(数据);
//调用回到这里更新您的看法
});
}
行动myController的
公共虚拟JsonResult MyAction(字符串数据)
{
//做的东西与你的数据在这里,这现在我的数据等于世界你好这个例子
返回JSON(ReturnSomeObject);
}
I have two user controls on the page and one of the user control has this text aread. which is used to add a note and but when they click add note button the page reloads. I do not want the page to reload ,i was looking for an example which this is done without postback.
Thanks
i tired doing this using JSON , but it throws the following error
The HTTP verb POST used to access path '/Documents/TestNote/Documents/AddNote' is not allowed.
<script type="text/javascript">
$(document).ready(function() {
$("#btnAddNote").click(function() {
alert("knock knock");
var gnote = getNotes();
//var notes = $("#txtNote").val();
if (gnote == null) {
alert("Note is null");
return;
}
$.post("Documents/AddNote", gnote, function(data) {
var msg = data.Msg;
$("#resultMsg").html(msg);
});
});
});
function getNotes() {
alert("I am in getNotes function");
var notes = $("#txtNote").val();
if (notes == "")
alert("notes is empty");
return (notes == "") ? null : { Note: notes };
}
</script>
</asp:Content>
Something like this would give you the ability to send data to an action do some logic and return a Json result then you can update your View accordingly.
Javascript Function
function AddNote(){
var d = new Date(); // IE hack to prevent caching
$.getJSON('/MyController/MyAction', { data: "hello world", Date: d.getTime() }, function(data) {
alert(data);
// call back update your view here
});
}
MyController Action
public virtual JsonResult MyAction(string data)
{
// do stuff with your data here, which right now my data equals hello world for this example
return Json("ReturnSomeObject");
}
这篇关于ASP.NET MVC两个用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!