ASP.Net MVC How to transfer data from a view to a controller

I am completely new to ASP.Net, and I am sure this is a very simple question. I have a view in which there is a link to create a report, but in order to be able to generate a report, I must ask the user to also specify a suitable text name.

Until now, I could transfer data from the server for viewing using models transferred from my controller for viewing, but I'm not sure how to transfer data from the view to my controller.

I just need to pass the string from the view to the controller in this case.

Any advice with an example would be appreciated.

UPDATE

I understand that I need to send data back to the server, but how to implement this in the form of a razorhtml code and controller?

+47
asp.net-mvc
Dec 02 '13 at 16:28
source share
3 answers

You can do this using ViewModels, for example, how you transferred data from your controller for viewing.

Suppose you have a viewmodel like this

public class ReportViewModel { public string Name { set;get;} } 

and in your GET action,

 public ActionResult Report() { return View(new ReportViewModel()); } 

and your view must be strictly printed on ReportViewModel

 @model ReportViewModel @using(Html.BeginForm()) { Report NAme : @Html.TextBoxFor(s=>s.Name) <input type="submit" value="Generate report" /> } 

and in your HttpPost method in your controller

 [HttpPost] public ActionResult Report(ReportViewModel model) { //check for model.Name property value now //to do : Return something } 

OR You can simply do this without the POCO classes (Viewmodels)

 @using(Html.BeginForm()) { <input type="text" name="reportName" /> <input type="submit" /> } 

and in your HttpPost action use a parameter with the same name as the text field name.

 [HttpPost] public ActionResult Report(string reportName) { //check for reportName parameter value now //to do : Return something } 

EDIT: According to the comment

If you want to send a message to another controller, you can use this overload of the BeginForm method.

 @using(Html.BeginForm("Report","SomeOtherControllerName")) { <input type="text" name="reportName" /> <input type="submit" /> } 

Passing data from an action method to view?

You can use the same view model, just set property values ​​to your GET action method

 public ActionResult Report() { var vm = new ReportViewModel(); vm.Name="SuperManReport"; return View(vm); } 

and in your opinion

 @model ReportViewModel <h2>@Model.Name</h2> <p>Can have input field with value set in action method</p> @using(Html.BeginForm()) { @Html.TextBoxFor(s=>s.Name) <input type="submit" /> } 
+77
Dec 02 '13 at 16:37
source share

If you do not want / do not post messages:

 @Html.ActionLink("link caption", "actionName", new { Model.Page }) // view controller @Html.ActionLink("link caption", "actionName", "controllerName", new { reportID = 1 }, null); [HttpGet] public ActionResult actionName(int reportID) { 

Please note that the reportID identifier in the new {} part corresponds to the reportID identifier in the action parameters, you can add any number of parameters this way, but no more than 2 or 3 (some will always argue), you must pass the model through POST (in accordance with another answer)

Change Zero added for correct overload as indicated in the comments. There are many overloads, and if you specify action + controller, then you will need both routeValues ​​and htmlAttributes. Without a controller (just a signature + action), only routeValues ​​parameters are needed, but it is best to always specify both.

+22
Dec 02 '13 at 16:37
source share
 <form action="myController/myAction" method="POST"> <input type="text" name="valueINeed"> <input type="submit" value="View Report"> </form> [HttpPost] public ActionResult myAction(string valueINeed) { .... } 
+10
Dec 02 '13 at 16:31
source share