I'm brand new to all things .NET. I have a very basic web page with an HTML form. I want 'onsubmit' to send the form data from the View to the Controller. I've seen similar posts to this but none have answers involving the new-ish Razor syntax. What do I do with 'onsubmit', and how do I access the data from the Controller? Thanks!!
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
You can wrap your view controls you want to pass in Html.Beginform.
For example:
When Submit button is pressed everything inside of of that Beginform will be submitted to your "ActionMethodName" method of the "ControllerName" controller.
ON the controller side you can access all received data from the view like this:
collection object above will contain all your input entries that we submitted from the form. You can access them by name just like you would access any array: collection["blah"] or collection.Get("blah")
You can also pass parameters to your controllers directly without sending the entire page with FormCollection:
Or you can combine both of these methods and pass specific parameters along with the Formcollection. It's up to you.
Hope it helps.
edit: while I was writing other users referred you to some helpful links as well. Take a look.
Defining a form in the following way:
@using (Html.BeginForm("ControllerMethod", "ControllerName", FormMethod.Post))
Will make a call to the method "ControllerMethod" in the controller "ControllerName". In the method you can accept a model, or other data types as inputs. See this tutorial for examples using forms and razor mvc.