Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC How to pass data from view to controller [closed]

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?

like image 565
Ahmed Avatar asked Dec 02 '13 16:12

Ahmed


People also ask

Can we pass data from view to controller in MVC?

This blog will discuss four (4) common ways to pass data from the view to the controller: Passing by Typed Arguments. Request Object. Form Collections Object.

How do you pass data from view to controller in MVC ViewBag?

ViewBag itself cannot be used to send data from View to Controller and hence we need to make use of Form and Hidden Field in order to pass data from View to Controller in ASP.Net MVC Razor.

Can I pass XML data from view to controller?

You cannot directly pass XML data as file to MVC controller. One of the best method is to pass XML data as Stream with HTTP post.


1 Answers

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" /> } 
like image 107
Shyju Avatar answered Oct 15 '22 07:10

Shyju