$_POST['color'] for MVC 4?

tezkovicz

New member
Joined
Nov 6, 2012
Messages
4
Programming Experience
3-5
Through an input field the color "green" is submitted.
Now, it takes requesting it.
In PHP it would be $_POST['color'].
I have thought it through, and have gotten it to this code:



The Example:

The Request for the ColorName - with VB.NET 2012 MVC 4:
Notice: The line breaks would not get preserved.

VB.NET:
    Public Class ColorPrintOutSubmitClassController<br />
        Inherits System.Web.Mvc.Controller<br />

        ' This method will handle GET<br />
        Function PrintOutPage() As ActionResult<br />
            Return View("PrintOutPage")<br />
        End Function<br />

        ' This method will handle POST<br />
        <HttpPost><br />
        Function ColorPrintOut() As ActionResult<br />
            ' Do something<br />
            Response.Write("You submitted the color: " & Request.QueryString("ColorName") & "<br />")<br />
            Return View()<br />
        End Function<br />
    End Class<br />

The HTML:

HTML:
<form action="" method="post">     <input type="text" name="ColorName" />     <input type="submit" name="ColorName_SubmitButton" value="Print It Out!" /> </form>

The Questions:

The problem in this attempt is that the color name does not get printed out, as wanted.


  1. Could the problem be rather on Reponse.Write, or Request.QueryString
  2. Also, how does get the part which is to handle the GET - "invoked"?



Invoked = to put into effect or operation (Source: The Merriam-Webster Dictionary)
 
HTML:
<form method="post"><asp:TextBox ID="ColorNameTextBox" runat="server" /><asp:Button ID="SubmitButton" Text="Print It Out!" /></form>
Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
  'Button click code
  'ColorNameTextBox is the textbox on the form
End Sub
 
I did almost think that your suggestion is directed towards the desktop environment, where I indeed had to take a second look.

Yet, I have to state that I have seen similar solutions as the one in your suggestion, though, I often could not "translate" it into VB.NET 2012, and have it succeed. Furthermore, a lot is also written for older versions, and also in C#.

I will have a closer look at your suggestion and give it a try - nevertheless, I have to state in beforehand that the "asp:" tag my not work in VB.NET 2012. Despite that, I do plan to attempt the try with an HTML button tag and the corresponding ID attribute.

EDIT:
I am getting this error message:
Error 25 Statement is not valid in a namespace.

As a notice, the whole "Private Sub" line is selected, by the error message, in Visual Studio 2012.

Do you have suggestions to this?
 
Last edited:
JB's solution is for Web Forms. As you're using MVC, it's of no use I'm afraid. Basically, don't use Response.Write in MVC. You can but you shouldn't. If you want to display something then put it in a view and display that view. Any data that needs to be variable should be part of the model. You create the model in the controller and pass it to the view, which then displays the data the model contains.
 
Any data that needs to be variable should be part of the model. You create the model in the controller and pass it to the view, which then displays the data the model contains.
Since you are addressing this. I am wondering, how the class (in the example of the first post), or perhaps the functions (each) do become invoked?

Basically, how to have the class invoked, by the models part of the MVC system?

With my PHP experience, I tend to think of - one, single page - being scanned with each line.
 
Last edited:
Back
Top