简体   繁体   中英

Print Text on Submit - VB.NET 2012, MVC 4, Visual Studio 2012

The Tools Used: VB.NET 2012, MVC 4, Visual Studio 2012

The Controller: SubmitFormController.vb

Namespace MvcApplication19
    Public Class UserNamePrintOutSubmitClassController
        Inherits System.Web.Mvc.Controller

        ' This method will handle GET
        Function Technology() As ActionResult
            Return View("Technology")
        End Function

        ' This method will handle POST
        <HttpPost>
        Function UserNamePrintOut() As ActionResult
            ' Do something
            Response.Write("Hello " & Request.QueryString("UserName") & "<br />")
            Return View()
        End Function
    End Class
End Namespace

The View: Technology.vbhtml

The URL: http://localhost/Home/Technology/

<form action="" method="post">
    <input type="text" name="UserName" />
    <input type="submit" name="UserName_submit" value="Print It Out!" />
</form>

The Question

I do not have a model in this example. The intention is to have the UserName submitted with the submit button and have it printed on the screen, on page load . Which means, the UserName should get passed to the action method and get printed on the screen.

I am having no error messages, nevertheless, the UserName does not get printed on the screen, perhaps, somebody, can have look at the code above.

I have been trying this with tutorials, which are often in C#. My background has been PHP, and I still tend to think in terms of "echo" - yet, I am getting used to MVC 4.

You're using ASP.NET MVC, not WebForms; however the concept of "postback" is unique to WebForms. It's like using System.Windows.Forms when you're actually using WPF.

In MVC you have different methods for each verb, you should rewrite it as follows:

Public Class SubmissionFormController
    Inherits System.Web.Mvc.Controller

    ' This method will handle GET
    Function UserNamePrintOut() As ActionResult
        Return View() ' Avoid using Response.Write in a controller action method, as the method is not being called in an appropriate place. Anything returned will be at the start of the response.
    End Function

    ' This method will handle POST
    <HttpPost>
    Function UserNamePrintOut(FormValueCollection post) As ActionResult
        ' Do something
        Return View()
    End Function

End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM