在提交中打印文本-VB.NET 2012,MVC 4,Visual Studio 2012



使用的工具: vb.net 2012,MVC 4,Visual Studio 2012

控制器: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

视图:technology.vbhtml

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>

问题

我在此示例中没有模型。目的是将UserName提交给提交按钮,并在屏幕上打印出on page load。这意味着,UserName应传递给action method并在屏幕上打印。

我没有错误消息,但是,UserName没有在屏幕上打印出来,也许有人可以查看上面的代码。

我一直在使用通常在C#中的教程尝试此操作。我的背景是PHP,我仍然倾向于用" Echo"来思考 - 但是,我已经习惯了MVC4。

您正在使用ASP.NET MVC,而不是WebForms;但是,"寄回"的概念是WebForms独有的。就像使用system.windows.forms实际上使用wpf。

在MVC中,您对每个动词都有不同的方法,应按照以下方式重写:

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

最新更新