89

I am completely new to ASP.Net and I am sure this is a very basic question I have a View in which there is a link to generate report but to be able to generate report I must ask the user to provide a suitable text name as well.

So far I have been able to pass data from server to view using Models passed from my controller to view, but I am not sure how to Pass data from view to my controller.

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

Any advice with example will be appreciated.

UPDATE

I understand I have to post the data back to server but how does that realize in the form of razorhtml code and controller?

2
  • the user has to post something - the view doesn't directly know how to communicate to a controller. Commented Dec 2, 2013 at 16:30
  • If you read the Microsoft documentation you will see that it does NOT explain how to do this. Commented May 6, 2019 at 12:01

3 Answers 3

147

You can do it with ViewModels like how you passed data from your controller to view.

Assume 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 strongly typed to ReportViewModel

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

and in your HttpPost action method in your controller

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

OR Simply, you can 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 same name as the textbox name.

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

EDIT : As per the comment

If you want to post to another controller, you may use this overload of the BeginForm method.

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

Passing data from action method to view ?

You can use the same view model, simply set the property values in your GET action method

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

and in your view

@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" />
}
Sign up to request clarification or add additional context in comments.

5 Comments

In the option/method 2 that you presented (without model) is there a way to specify the controller name ? because the controller that should receive this post request is different than controller of this view..
@Ahmed: Check the updated answer. You can use the specific overload of BeginForm method.
Nice, I've been looking for a few hours now. Thanks!
Thanks!! Very clear answer. For my case, in the HttpPost action, I didn't need to use a parameter with same name as the textbox name. Only put model object as parameter, I can be able to retrieve the values.
One can also use Ajax post to send data to action. Also see this link for various ways to post data c-sharpcorner.com/UploadFile/3d39b4/…
36

In case you don't want/need to post:

@Html.ActionLink("link caption", "actionName", new { Model.Page })  // view's controller
@Html.ActionLink("link caption", "actionName", "controllerName", new { reportID = 1 }, null);

[HttpGet]
public ActionResult actionName(int reportID)
{

Note that the reportID in the new {} part matches reportID in the action parameters, you can add any number of parameters this way, but any more than 2 or 3 (some will argue always) you should be passing a model via a POST (as per other answer)

Edit: Added null for correct overload as pointed out in comments. There's a number of overloads and if you specify both action+controller, then you need both routeValues and htmlAttributes. Without the controller (just caption+action), only routeValues are needed but may be best practice to always specify both.

1 Comment

Just FYI to anyone who comes across this simple answer, add a null after including your route values [ new { reportID = 1 }], otherwise it'll try to take them as htmlAttributes and they won't come through,
26
<form action="myController/myAction" method="POST">
 <input type="text" name="valueINeed" />
 <input type="submit" value="View Report" />
</form> 

controller:

[HttpPost]
public ActionResult myAction(string valueINeed)
{
   //....
}

2 Comments

<input type="valueINeed" type="text"> should be <input name="valueINeed" type="text">
Thnx. Updated response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.