1
好吧,让我开始说我是MVC的全新。因此,我使用Visual Studio 2010附带的MVC版本。这也是使用Visual Basic,但我相信一旦你看到代码就可以搞清楚。验证后MVC表单数据丢失
我想验证我的数据,验证过程工作正常。但是,如果验证失败,我的文本框中的值不会重新加载到表单上。
我的表单相当简单,因为我想从我的第一个应用程序简单的东西开始。它由一个绑定到协议对象集合的表组成。在表格的底部,我有两个用于添加新协议的文本框。正是这两个文本框没有将数据发回给他们。
这里是视图:
<%@ 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
嗯......这是一个使用母版页的视图。没有BeginForm方法,因为表单实际上在母版页中。如果我要从母版页中移动表单,是否可以在此视图中创建两个表单以使其按照我想要的方式工作?我的应用程序已经使用了多年的datagrid样式布局,这是我的用户习惯的。 – fizch 2010-11-03 13:56:31
是的..我认为你可以在这个视图上有两种形式(但不能嵌套)。 – Claudiu 2010-11-03 16:29:06
此外,您还可以为您的视图创建新的视图模型,该视图将为您当前的模型列表创建一个属性,并为您的模型创建一个新帐户(或您创建的帐户)的属性 – Claudiu 2010-11-03 16:30:59