好的,首先让我说我是MVC的新手。有了这个,我使用Visual Studio 2010附带的MVC版本。这也是使用Visual Basic,但我相信你可以在看到代码后想出来。
我正在尝试验证我的数据,验证过程正常运行。但是,如果验证失败,则表单中不会重新加载文本框中的值。
我的表单相当简单,因为我想从第一个应用程序的简单开始。它由一个与Agreement对象集合相关联的表组成。在表格的底部,我有两个用于添加新协议的文本框。正是这两个文本框没有将数据发回给他们。
以下是观点:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Food.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of NdaToolLibrary.BusinessLayer.Agreement))" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PrimaryContent" runat="server">
<h1>Agreement List</h1>
<table cellpadding="0" cellspacing="0" border="0" class="tableDefault" style="width: 300px !important">
<tr>
<th>
AccountType
</th>
<th>
NationalId
</th>
<th></th>
</tr>
<% For i As Integer = 0 To Model.Count - 1%>
<tr class="<%= IIF(i mod 2 = 0, "", "detailRow") %>">
<td>
<%: Model(i).AccountType%>
</td>
<td>
<%: Model(i).NationalId%>
</td>
<td>
<%: Html.RouteLink("Delete", "Delete", New With {.action="Delete", .acctType = Model(i).AccountType, .id = Model(i).NationalId})%>
</td>
</tr>
<% Next%>
<tr class="footerRow">
<td>
<%= Html.TextBox("AccountType")%>
<%= Html.ValidationMessage("AccountType", " ")%>
</td>
<td>
<%= Html.TextBox("NationalId")%>
<%= Html.ValidationMessage("NationalId"," ")%>
</td>
<td>
<a href="javascript: document.forms[0].action='/Agreement/Create'; document.forms[0].submit();">Create</a>
</td>
</tr>
</table>
<%= Html.ValidationSummary("Could not save agreement. Reasons include:", New with{.class = "red"})%>
</asp:Content>
这是控制器:
Namespace Web
Public Class AgreementController
Inherits System.Web.Mvc.Controller
Public Function Create(ByVal FormValues As FormCollection) As ActionResult
Dim agreement As New BusinessLayer.Agreement()
agreement.AccountType = FormValues("AccountType").ToString()
agreement.NationalId = FormValues("NationalId").ToString()
If agreement.IsValid Then
Try
agreement.Save()
Catch ex As Exception
ModelState.AddModelError("", ex.Message)
End Try
Else
For Each validationError As BusinessLayer.DataValidationMessage In agreement.GetValidationResults()
ModelState.AddModelError(validationError.Property, validationError.Message)
Next
End If
'Try
' agreement.AccountType = FormValues("AccountType").ToString()
' agreement.NationalId = FormValues("NationalId").ToString()
' agreement.Save()
'Catch dvEx As BusinessLayer.DataValidationException
' For Each validationError As BusinessLayer.DataValidationMessage In dvEx.ValidationMessages
' ModelState.AddModelError(validationError.Property, validationError.Message)
' Next
'Catch ex As Exception
' ModelState.AddModelError("", ex.Message)
'End Try
Dim agreements As New BusinessLayer.AgreementCollection()
agreements.Load()
Return View("Index", agreements)
End Function
Public Function Delete(ByVal AcctType As String, ByVal Id As String) As ActionResult
Dim agreement As New BusinessLayer.Agreement()
agreement.AccountType = AcctType
agreement.NationalId = Id
Return View("Delete", agreement)
End Function
<AcceptVerbs(HttpVerbs.Post)> _
Public Function Delete(ByVal AcctType As String, ByVal Id As String, ByVal FormValues As FormCollection) As ActionResult
Dim agreement As New BusinessLayer.Agreement(AcctType, Id)
agreement.Delete()
Return RedirectToAction("Index")
End Function
Public Function Index() As ActionResult
Dim agreements As New BusinessLayer.AgreementCollection()
agreements.Load()
' Return View("Index", agreements) is implied because the view has the same name as the action
Return View(agreements)
End Function
End Class
End Namespace
答案 0 :(得分:3)
首先,我认为你应该提取你的字段需要在一个单独的视图中创建一个新帐户(现在它在主页面中)。您可以通过在母版页视图上放置一个链接来访问此新视图。
您的新视图应该是强类型的(您需要为它创建一个模型)。
并在新视图页面中使用(Html.BeginForm()){}语句创建帐户所需的字段。
在控制器上你必须有像
public void Create(YourModelType model)
{
if (!ModelState.IsValid) //this will check if there are validation errors after binding
{
return View(model); //this will repopulate back the input collected from the form
}
//here you can put your code to process the creation
}
很抱歉给你举例C#,但我不懂VB。
希望我的回答可以帮到你。