asp.net mvc - Forget about forms - How to update the server when any textbox changes?
I have a page full of text boxes (Fields). Each containing data. I would like to automatically update the server (just the specific field) every time the user changes and leaves a text box.
I would like to be able to give a generic solution using one controller action and not write specific actions for each field.
Do you know of a pattern/documentation/plugin/SO Question - that I can use.
I found on SO:
how-to-call-a-server-side-method-on-losing-focus-from-a-textbox-asp-net-mvc- It might be good as a start on the client side, but maybe there are other options?Edit:
OK I though of down voting my own question. I guess the message didn't come across as expected.I have an application with many partial controls开发者_StackOverflow in them. (loaded with ajax)
Each time the user wants to edit a record he has to complete the process by submitting the form he is in.
The user has asked to save the fields automatically, this will suppress the need for an extra action of submitting - If you think about it - he is actually asking to "submit" every field when it looses focus. I am looking for a way to do this with a single controller action.
Hope this explains better - happy new year.
Thanks in advance and enjoy life, Julian
There is no other option. ASP.NET MVC is a server-side technology only. Also, it is stateless (with the exception of session state) - once a web page is delivered, it "forgets" about everything related to that page. In fact ASP.NET MVC is not even aware of what exactly was contained in that page (be it a text input or just some divs). There are no components and postbacks as you might be used to from ASP.NET Webforms.
Therefore If you want to react on client-side events, you need to handle them with Javascript.
What you could do if you really want this behavior for the entire site is to write a document.ready function (assuming that you are using jquery) that is situated in a javascript file and referenced by a master page used by all views. This way you would implement this behavior without any custom coding for each view.
I'd guess that you actually don't want this behavior for all text boxes, so you could give those text boxes one special class and only select them in your javascript method.
精彩评论