0

使用的工具: 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

网址: 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”的角度来思考——然而,我已经习惯了 MVC 4。

4

1 回答 1

2

您使用的是 ASP.NET MVC,而不是 WebForms;然而,“回发”的概念是 WebForms 独有的。这就像在实际使用 WPF 时使用 System.Windows.Forms。

在 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
于 2012-11-06T03:35:36.310 回答