MVC将Model传递给Controller问题

时间:2013-10-18 18:17:18

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

代码是VB,使用MVC 4(可能是5) 我试图弄清楚我的代码有两个问题,到目前为止,我找不到任何导致我当前问题的问题。 首先,当模型被传回时,似乎什么都没有(包含在内的所有元素都没有)。这是一个问题。我已经在线查看并且无法找到解决方案(或者建议问题是什么)。 其次,当其中一个按钮被击中时,它不会立即进入正确的HttpPost方法,而是循环通过它们(以不稳定的顺序)。这也是一个问题。

这是控制器。模型将通过索引传递给View,标记为httpget。

Imports System.Data.SqlClient
Imports System.Web.Mvc
Imports System.Web.Routing
Imports System.ComponentModel.DataAnnotations

Namespace Test
    <HttpPost()> _
    Function SelectUser(ByVal Model As Test.Models.AdminModel) As ActionResult
        Return View("Index", Model)
    End Function

    <HttpPost()> _
    Function AddEnv(ByVal Model As Test2.Models.AdminModel) As ActionResult
        Return View("Index", Model)
    End Function

    <HttpPost()> _
    Function RemoveEnv(ByVal Model As Test.Models.AdminModel) As ActionResult
        Return View("Index", Model)
    End Function

    <HttpPost()> _
    Function AddApp(ByVal Model As Test.Models.AdminModel) As ActionResult
        Return View("Index", Model)
    End Function

    <HttpPost()> _
    Function RemoveApp(ByVal Model As Test.Models.AdminModel) As ActionResult
        Return View("Index", Model)
    End Function
End Namespace

这是视图。该模型似乎正确绑定(我得到intellisense)

@ModelType Test.Test.Models.AdminModel
@Code
ViewData("Title") = "Admin"
Layout = "~/Views/Shared/_Admin.vbhtml"
End Code  

<table>
<tr>
    @Using (Html.BeginForm("SelectUser", "Admin", method:=FormMethod.Post))
        @<td>Username:&nbsp;</td>
        @<td>@Html.TextBoxFor(Function(Model) Model.UserAccount.Username)</td>
        @<td><a href="javascript:$('form').submit();" class='BUTTON' style="display:   inline-block; height: 30px; width: 80px"><span>Select User</span></a></td>
        @<td><input type ="submit" Class="BUTTON" style="display: inline-block; height:   30px; width: 80px" value ="Login"/></td>
    End Using
</tr>

<tr>
    @Using (Html.BeginForm("AddApp", "Admin", method:=FormMethod.Post))
        @<td>Add App:&nbsp;</td>
        @<td>@Html.TextBoxFor(Function(Model) Model.NewApp)</td>
        @<td><a href="javascript:$('form').submit();" class='BUTTON' style="display: inline-block; height: 30px; width: 80px"><span>Add App</span></a></td>
        @<td><input type ="submit" Class="BUTTON" style="display: inline-block; height: 30px; width: 80px" value ="Login"/></td>
    End Using

</tr>
<tr>
    @Using (Html.BeginForm("RemoveApp", "Admin", method:=FormMethod.Post))
        @<td>Remove App:</td>
        @<td>@Html.DropDownListFor(Function(Model) Model.AppList, Model.AppList)</td>
        @<td><a href="javascript:$('form').submit();" class='BUTTON' style="display: inline-block; height: 30px; width: 80px"><span>Remove App</span></a></td>
        @<td><input type ="submit" Class="BUTTON" style="display: inline-block; height: 30px; width: 80px" value ="Login"/></td>
    End Using
</tr>

<tr>
    @Using (Html.BeginForm("AddEnv", "Admin", method:=FormMethod.Post))
        @<td>Add Env:&nbsp;</td>
        @<td>@Html.TextBoxFor(Function(Model) Model.NewEnv)</td>
        @<td><a href="javascript:$('form').submit();" class='BUTTON' style="display: inline-block; height: 30px; width: 80px"><span>Add Env</span></a></td>
        @<td><input type ="submit" Class="BUTTON" style="display: inline-block; height: 30px; width: 80px" value ="Login"/></td>
    End Using
</tr>
<tr>
    @Using (Html.BeginForm("RemoveEnv", "Admin", method:=FormMethod.Post))
        @<td>Remove Env:</td>
        @<td>@Html.DropDownListFor(Function(Model) Model.EnvList, Model.EnvList)</td>
        @<td><a href="javascript:$('form').submit();" class='BUTTON' style="display: inline-block; height: 30px; width: 80px"><span>Remove Env</span></a></td>
        @<td><input type ="submit" Class="BUTTON" style="display: inline-block; height:     30px; width: 80px" value ="Login"/></td>
    End Using
</tr>

</table>

最后,模型:

Imports System.ComponentModel.DataAnnotations
Namespace Test.Models
Public Class AdminModel
    <Required(ErrorMessage:="User Name field is required"), Display(Name:="User Name")>
    Public UserAccount As User2.User
    Public AppList As SelectList
    Public EnvList As SelectList
    Public PermissionsTable As DataTable
    Public NewEnv As String
    Public NewApp As String
End Class
End Namespace

1 个答案:

答案 0 :(得分:0)

您应该在模型中使用属性,而不是公共变量。

Public Property UserAccount As User2.User
    Public Property AppList As SelectList
    Public Property EnvList As SelectList
    Public Property PermissionsTable As DataTable
    Public Property NewEnv As String
    Public Property NewApp As String