Click here to Skip to main content
15,886,535 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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?]
Posted
Updated 17-Nov-14 19:12pm
v3

1 solution

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):
JavaScript
function noenter() {
     return !(window.event && window.event.keyCode == 13); 
}

Then, in your view, modify your text area and link it with this JS function:
HTML
<textarea cols="4" rows="4" class="col-sm-4" id="txtChatConversation" onkeypress="return noenter();"></textarea>
 
Share this answer
 
v5
Comments
Jineesh TR 18-Nov-14 2:41am    
Actually my project is in MVC4. So how can I do this... Please help me..
Raul Iloc 18-Nov-14 2:58am    
Like is indicated in the link above, you should use JavaScript for disabling/overwriting the default behavior on button click in your text area. I will add details in a 3rd point in my solution above.
Jineesh TR 18-Nov-14 6:59am    
Hi Raul Illoc,
This code works fine for me. But when I press F5, submit button action is again get triggered.
Thanks in advance..
Raul Iloc 18-Nov-14 7:30am    
In the browser, F5 key means refresh page, and this means repeat the last action. See the next: http://www.codeproject.com/Questions/434697/In-ASP-NET-After-clicking-on-Browser-Refresh-F-bu
Jineesh TR 18-Nov-14 8:13am    
Thank you Raul Illoc,...
It is working fine...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900