如何使用javascript在MVC4中触发事件? [英] How do I trigger an event in MVC4 using javascript?

查看:87
本文介绍了如何使用javascript在MVC4中触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



请帮帮我..

我坚持使用此代码。

我必须在我的项目中做聊天应用程序。

为此候选人应该注册。一旦注册结束,聊天文本框将显示给他/她聊天。



为此,我创建了一个包含两个div的视图。一个包含注册表单,另一个包含聊天文本框。

我以编程方式一次显示一个div。



提交注册我用注释HttpPost编写了一个动作方法Index。

为了更新回车键按下事件中的聊天数据,我在javascript文件中编写了代码。在那里我检查了enter键的出现。为了更新数据,我在控制器SubmitChat中编写了一个动作方法。



注册正确完成,聊天文本框可见。但是当我键入一些文本并按回车键时会出现问题。当我按下输入时,聊天文本保存(即它调用动作方法SubmitChat)。但保存聊天文本后,它会自动重定向到操作方法索引(带注释HttpPost),然后候选人再次注册(再次保存)。



我不知道是什么发生在那里..以及如何去那里..请看代码的链接.. [如何在控制器MVC4中使用两个post方法?]

解决方案
1。当您在任何网络表单中按Enter键时,等同于提交按钮单击,因此整个页面将提交给控制器,在您的情况下,

索引< br /> 

方法被调用以管理保存。



2.解决方案是禁用此默认行为,并强制用户在他/她想要提交时按下按钮这页纸。在这里您可以找到详细信息: http://forums.asp.net/t/1227095.aspx [< a href =http://forums.asp.net/t/1227095.aspxtarget =_ blanktitle =新窗口> ^ ]



3.您应该在视图中添加下一个JavaScript函数(或.js文件):

  function  noenter(){
return !( window .event&& window .event.keyCode == 13 );
}



然后,在您的视图中,修改您的文本区域并将其与此JS函数链接:

 <   textarea    < span class =code-attribute> cols   =  4     =  4    class   =  col-sm-4    id   =  txtChatConversation    onkeypress   =  return noenter();  >  <   / textarea  >  


Hi All,

Please help me..
I have stuck with this code.
I have to do chat application in my project.
For that the candidate should register. once registration is over a chat text box will be visible for him/her to chat.

For this I have created a view which contains two div. one contains registration form and other contains chat text box.
I made one of the div visible at a time programmatically.

For submit registration I wrote an action method Index with annotation HttpPost.
And for updating chat data in the enter key press event, I wrote code in the javascript file. there I checked for the occurrence of enter key. For updating the data I wrote an action method in the controller SubmitChat.

registration completes properly and the chat text box visible. But problem arises when I types some text and press enter. When I press enter the chat text saves (ie it calls the action method SubmitChat). But after saving chat text it automatically redirected to the action method Index (with annotation HttpPost) and again the candidate is registering (saving again).

I dont know what is happening there.. and how it goes there.. Please see the link for the code..[How do I use two post methods in a controller MVC4?]

解决方案

1. When you press enter in any web form, is equivalent with submit button click, so the entire page is submitted to the controller and in your case the

Index <br />

method is invoked for manage the saving.

2.The solution is to disable this default behavior, and to force the user to press the button if he/she want to submit the page. Here you could find the details: http://forums.asp.net/t/1227095.aspx[^]

3.You should add the next JavaScript function in your view ( or .js file):

function noenter() {
     return !(window.event && window.event.keyCode == 13); 
}


Then, in your view, modify your text area and link it with this JS function:

<textarea cols="4" rows="4" class="col-sm-4" id="txtChatConversation" onkeypress="return noenter();"></textarea>


这篇关于如何使用javascript在MVC4中触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆