0

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

这是控制器。模型通过索引传递给视图,索引标记为 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

这是视图。模型似乎正确绑定(我得到了智能感知)

@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
4

1 回答 1

0

You should use properties in your model, not public variable.

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
于 2013-10-18T18:24:37.483 回答