This is a a low level mechanism that parses the http request and expose posted form fields. Common problems are :
- this is not a POST request
- note that form fields are not posted if they are not inside the form tag, not enabled or don't have a name attribute
Or could it be in how you then transfert control to your ASPX page. If confirmed you'll perhaps need to copy the content of this collection rather than to just use a reference to the original Request.Form.
(I would really extract the whole processing from those pages so that it could be called either from an ASPX page, CSHTML page, MVC controller etc...).
All of the examples I have seen depend on a fixed number of fields. Unfortunately, the form that gets generated from the model can vary in the number and type of fields. I may have to go back and do it in webforms, which I don't have an issue with, but my
boss would prefer it be done in MVC.
It should work as well with MVC or Razor pages. Once again this is a low level mechanism that allows to expose form fields posted by the current http request (you had the same already in ASP Classic 20 years ago). You could try a Razor test page as shown
in the earlier link and it should work
For now I suspect that either form fields are not posted correctly (try perhaps F12 Network and inspect the posted payload) or the ASPX page is called in a way that doesn't allow to carry over the Request.Form collection coming from the Razor page (in which
case you may have to copy that to your own collection rather than point to the original collection).
How do you transfer control from the Razor page to the ASPX page ? You are using Response.Redirect maybe ?
When going from ASPX to ASPX I user Server.Transfer, but I can't seem to find the MVC equivalent? I'm new to this so please bear with my lack of knowledge :)
When going from ASPX to ASPX I user Server.Transfer, but I can't seem to find the MVC equivalent? I'm new to this so please bear with my lack of knowledge :)
There are two ways to pass data; in the URL (HTTP GET) or in the message body (HTTP POST). You can modify my example above to POST to the an ASPX form. Your original ASPX code will work if placed in the Page_Load event.
This seems to work fine when the fields are clearly defined, but as I mentioned earlier, the number and type and name and ID of the fields are dynamic. I can't simply fill in specific fields, I need to be able to capture as many as are checked on the form,
just like I do with the web form.
This seems to work fine when the fields are clearly defined, but as I mentioned earlier, the number and type and name and ID of the fields are dynamic. I can't simply fill in specific fields, I need to be able to capture as many as are checked on the form,
just like I do with the web form.
The source of the data is irrelevant. If the source is dynamic in ASPX then it should be dynamic in Razor Pages too. Is your actual question how to create dynamic inputs in Razor Pages?
Not at all. I have the CSHTML page creating exactly what I am looking for based on the specific requirements using the models for each individual component. So the issue isn't getting the dynamic data created, it's getting the checked dynamic data to be
passed through to an aspx page the processes that data (that page currently exists, is huge, and would be an enormous task to rewrite it into a different format page.)
Not at all. I have the CSHTML page creating exactly what I am looking for based on the specific requirements using the models for each individual component. So the issue isn't getting the dynamic data created, it's getting the checked dynamic data to be
passed through to an aspx page the processes that data (that page currently exists, is huge, and would be an enormous task to rewrite it into a different format page.)
I don't get it. If you created the dynamic inputs as stated then there is no problem. What is checked dynamic data and why are you unable to pass this data?
Perhaps you need do the GET or POST in code? JavaScript? C#?
Can you help me understand that after the user chooses their items, how do I POST all of the selected items to the next page (ASPX) for processing and validation? Preferably by clicking a submit button that fires code behind but whatever method
will post the form values I will work with :)
Your struggling with HTML Forms. ASP.NET Web Forms hides a lot HTTP functionality behind server controls. Many ASP.NET web forms developers struggle when moving to MVC because they do not understand the HTTP protocol. The following HTML should fix your
issue (I did not test it) or at least get you closer to a solution. Keep in mind there is only one input within your code snippet.
First, I added an HTML form. You can't have nested HTML forms so if there is an outer forum element that surrounds your original code snippet, you might need to rethink the general design. I made the
input name dynamic - data driven. In HTML Web Forms, the element
name/value is submitted to the server not the ID/value. Web forms hides this fact. I removed the runat="server" because MVC does have server controls. Lastly, I added a
submit button within the form which submits the form fields to the
form's action attribute; the aspx page.
OK so I followed your advice, but no matter how many different permutations I tried, getting the form data from the cshtml page to the aspx server side code was still null.
HOWEVER, I decided to try redirecting to a different controller instead of the aspx page. Lo and behold I now have every bit of data that I need!
Thank you for your patience and direction towards the solution! :)
None
0 Points
7 Posts
How to post CSHTML form data to ASPX processor
Mar 10, 2020 12:02 PM|jgarniss|LINK
Hi all,
I currently have a webforms project that passes the values in a dynamically created aspx page to a processor aspx page as follows:
System.Collections.Specialized.NameValueCollection postedValues = Request.Form;
This gives me all of the selected values on the webform and I go through a validation process and then move on to the next process.
I am trying to replicate the process so as to not reinvent the wheel but the initial page is CSHTML. When I do the request.form the value is empty.
How do I go about getting the same functionality?
TIA!
Jon
All-Star
48570 Points
18079 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 12:39 PM|PatriceSc|LINK
Hi,
Not 100% sure to get your design. Does it work from within your cshtml page ? See perhaps https://docs.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes/4-working-with-forms
This is a a low level mechanism that parses the http request and expose posted form fields. Common problems are :
- this is not a POST request
- note that form fields are not posted if they are not inside the form tag, not enabled or don't have a name attribute
Or could it be in how you then transfert control to your ASPX page. If confirmed you'll perhaps need to copy the content of this collection rather than to just use a reference to the original Request.Form.
(I would really extract the whole processing from those pages so that it could be called either from an ASPX page, CSHTML page, MVC controller etc...).
None
0 Points
7 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 01:14 PM|jgarniss|LINK
All of the examples I have seen depend on a fixed number of fields. Unfortunately, the form that gets generated from the model can vary in the number and type of fields. I may have to go back and do it in webforms, which I don't have an issue with, but my boss would prefer it be done in MVC.
All-Star
53081 Points
23649 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 01:36 PM|mgebhard|LINK
MVC uses model binding. See the MVC fundamentals for details.
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-3.1
[HttpGet] public IActionResult Index() { return View(); } [HttpPost] public IActionResult Index(ContactModel model) { return Json(model); }
All-Star
48570 Points
18079 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 01:42 PM|PatriceSc|LINK
It should work as well with MVC or Razor pages. Once again this is a low level mechanism that allows to expose form fields posted by the current http request (you had the same already in ASP Classic 20 years ago). You could try a Razor test page as shown in the earlier link and it should work
For now I suspect that either form fields are not posted correctly (try perhaps F12 Network and inspect the posted payload) or the ASPX page is called in a way that doesn't allow to carry over the Request.Form collection coming from the Razor page (in which case you may have to copy that to your own collection rather than point to the original collection).
How do you transfer control from the Razor page to the ASPX page ? You are using Response.Redirect maybe ?
None
0 Points
7 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 01:46 PM|jgarniss|LINK
When going from ASPX to ASPX I user Server.Transfer, but I can't seem to find the MVC equivalent? I'm new to this so please bear with my lack of knowledge :)
All-Star
53081 Points
23649 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 02:40 PM|mgebhard|LINK
There are two ways to pass data; in the URL (HTTP GET) or in the message body (HTTP POST). You can modify my example above to POST to the an ASPX form. Your original ASPX code will work if placed in the Page_Load event.
You can also use a link.
You cannot use Server.Transfer from Razor Pages to ASPX.
HTTP GET and POST
https://www.w3schools.com/tags/ref_httpmethods.asp
None
0 Points
7 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 03:34 PM|jgarniss|LINK
This seems to work fine when the fields are clearly defined, but as I mentioned earlier, the number and type and name and ID of the fields are dynamic. I can't simply fill in specific fields, I need to be able to capture as many as are checked on the form, just like I do with the web form.
All-Star
53081 Points
23649 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 03:59 PM|mgebhard|LINK
The source of the data is irrelevant. If the source is dynamic in ASPX then it should be dynamic in Razor Pages too. Is your actual question how to create dynamic inputs in Razor Pages?
None
0 Points
7 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 04:02 PM|jgarniss|LINK
Not at all. I have the CSHTML page creating exactly what I am looking for based on the specific requirements using the models for each individual component. So the issue isn't getting the dynamic data created, it's getting the checked dynamic data to be passed through to an aspx page the processes that data (that page currently exists, is huge, and would be an enormous task to rewrite it into a different format page.)
All-Star
53081 Points
23649 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 10, 2020 05:23 PM|mgebhard|LINK
I don't get it. If you created the dynamic inputs as stated then there is no problem. What is checked dynamic data and why are you unable to pass this data?
Perhaps you need do the GET or POST in code? JavaScript? C#?
None
0 Points
7 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 11, 2020 01:13 PM|jgarniss|LINK
OK so again, please bear with me, I am new to MVC coding and used to Webforms using server side coding for the most part.
Here is a snippet of the CSHTML page that is being populated by a model populated in the controller. I can choose one radio button per itemDetail.
Can you help me understand that after the user chooses their items, how do I POST all of the selected items to the next page (ASPX) for processing and validation? Preferably by clicking a submit button that fires code behind but whatever method will post the form values I will work with :)
TIA
Jon
All-Star
53081 Points
23649 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 11, 2020 02:11 PM|mgebhard|LINK
Your struggling with HTML Forms. ASP.NET Web Forms hides a lot HTTP functionality behind server controls. Many ASP.NET web forms developers struggle when moving to MVC because they do not understand the HTTP protocol. The following HTML should fix your issue (I did not test it) or at least get you closer to a solution. Keep in mind there is only one input within your code snippet.
First, I added an HTML form. You can't have nested HTML forms so if there is an outer forum element that surrounds your original code snippet, you might need to rethink the general design. I made the input name dynamic - data driven. In HTML Web Forms, the element name/value is submitted to the server not the ID/value. Web forms hides this fact. I removed the runat="server" because MVC does have server controls. Lastly, I added a submit button within the form which submits the form fields to the form's action attribute; the aspx page.
I recommend learning HTTP GET and POST which is a fundamental construct for designing web application that accept user input.
None
0 Points
7 Posts
Re: How to post CSHTML form data to ASPX processor
Mar 11, 2020 04:18 PM|jgarniss|LINK
OK so I followed your advice, but no matter how many different permutations I tried, getting the form data from the cshtml page to the aspx server side code was still null.
HOWEVER, I decided to try redirecting to a different controller instead of the aspx page. Lo and behold I now have every bit of data that I need!
Thank you for your patience and direction towards the solution! :)
Jon