One department in our company is using classic asp.net while ours department is using MVC.
We need to pass 5 variables to his page (by form submit). Can someone please show a simple example of posting form data from an MVC controller to an asp.net page expecting the form variable?
To post an ASP.NET Web page to another pageAdd a button control to your Web page, such as a Button, LinkButton, or ImageButton control. Set the PostBackUrl property for the control to the URL of the page to which you want to post the ASP.NET Web page.
ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary.
The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.
If I am reading this correctly, you should be able to do this without any cross domain / application concern. You want to do this in the controller, so you can use the HttpWebRequest class to post the data. It's conceptually the same as posting from a web browser as far as the target application is concerned.
Here's a quick and dirty snippet:
// name / value pairs. field names should match form elements
string data = field2Name + "=" + field1Value + "&" + field2Name+ "=" + field2Value
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(<url to other applications form action>);
// set post headers
request.Method = "POST";
request.KeepAlive = true;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
// write the data to the request stream
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
// iirc this actually triggers the post
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With