代码是 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: </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: </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: </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