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

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で、今でも「エコー」の観点から考える傾向がありますが、MVC4には慣れています。

4

1 に答える 1

2

WebForms ではなく、ASP.NET MVC を使用しています。ただし、「ポストバック」の概念は 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 に答える