从Webforms到MVC 4的一些基础知识

时间:2015-05-26 19:37:41

标签: asp.net-mvc vb.net webforms

习惯Webforms我遇到了问题'可视化' MVC

如果我创建一个类(名为Customers.vb)并添加一个字符串属性

Public Property Customers_CustomerName() As String

...将此功能添加到控制器

 Function Index() As ActionResult
        Dim vCustomers As New Customers

        vService = New Service1Client
        strSQL = "SELECT Cust_Name FROM Customers WHERE Customer_ID = 1"
        Dim vName As String = vService.ReturnScalarString(strSQL, 2)

        vCustomers.Customers_CustomerName = vName
        vService.Close()
        vService = Nothing

        ViewData("Customers") = vCustomers
        Return View("Customers_View")
    End Function

...并将其添加到视图

 Code
        Dim vCustomers As MVPTest.Customers = CType(ViewData("Customers"), MVPTest.Customers)
        Dim CustName As String = vCustomers.Customers_CustomerName
        Using (Html.BeginForm(Nothing, "Customers_/Edit/5", FormMethod.Post))
        @<p>
            @Html.TextBox("TB3", CustName)
            <input type="submit" value="test" />
        </p>

        End Using

    End Code

我现在在文本框中获得了值 - 但与WebForms相比,这似乎是一个非常漫长的过程,其中代码隐藏只是&#34; TB2.value = vName&#34; (其中文本框是一个通用的输入控件,使用Runat =&#34; Server&#34; - 所以我必须遗漏一些东西。

此外,我找不到将文本框中的值回发回控制器的方法

1 个答案:

答案 0 :(得分:0)

<HttpPost()>
    Function Edit(ByVal id As Integer, ByVal collection As FormCollection) As ActionResult
        Try
            Dim EditName As String = Convert.ToString(collection("TB3").ToString)


            Return RedirectToAction("Index")
        Catch
            Return View()
        End Try
    End Function