问题
我有一个搜索结果页面,其中列出了搜索条件元素并允许用户单击一个元素来删除该搜索条件。
第一次单击其中一个标准元素将其删除时,一切都按预期工作。
但是,第二次删除条件元素时,第一个删除的元素又回来了。
例子
搜索条件: {topic: "abuse"}, {profession: "admin"}, {keywords: "animal"}
现在单击“主题”元素将其删除,结果页面现在显示{profession: "admin"}, {keywords: "animal"}
现在单击“关键字”将其删除,页面现在显示不正确{topic: "abuse"}, {profession: "admin"}
此时正确的显示应该只有一个标签{profession: "admin"}
代码
SearchController.Results()
<HttpPost>
<NoCache>
Function Results(ByVal model As SearchCriteria) As ActionResult
Dim resultsModel As SearchResultsModel = New SearchResultsModel
Dim searchQuery As IQueryable(Of [Class]) = Nothing
Dim searchResults As List(Of [Class]) = Nothing
Dim categoryValueQuery As IQueryable(Of CourseCategoryValue) = Nothing
Dim query As IQueryable(Of [Class]) = Nothing
Dim queries As List(Of IQueryable(Of [Class])) = New List(Of IQueryable(Of [Class]))
model = RemoveCriteria(model)
model.RemovalCriteria = String.Empty
resultsModel.Criteria = model
Return View(resultsModel)
End Function
SearchController.RemoveCriteria()
<NonAction>
Function RemoveCriteria(ByVal model As SearchCriteria) As SearchCriteria
Dim result As SearchCriteria = ObjectMapper.Duplicate(model)
If Not String.IsNullOrEmpty(result.RemovalCriteria) Then
Select Case result.RemovalCriteria.ToLower.Trim
Case "title"
result.Title = String.Empty
Case "keywords"
result.Keywords = String.Empty
Case "healthtopic"
result.HealthTopic = String.Empty
Case "city"
result.City = String.Empty
Case "profession"
result.Profession = String.Empty
End Select
End If
Return result
End Function
结果.vbhtml
@Imports System.Reflection
@ModelType SearchResultsModel
@Code
ViewData("Title") = "Results"
Dim properties As List(Of PropertyInfo) = Model.Criteria.GetType.GetProperties.ToList
Dim names() As String = {"keywords", "healthtopic", "profession"}
End Code
<h2>Results</h2>
@If (Not Nothing Is Model) AndAlso (Not Nothing Is Model.Results) Then
@<h3>Displaying @Model.Results.Count of @Model.ResultCount classes</h3>
End If
@Using Html.BeginForm("Results", "Search", Nothing, FormMethod.Post, New With {.id = "removalForm"})
@Html.Hidden("RemovalCriteria", String.Empty, New With {.id = "removalCriteria"})
@<div>
<span style="font-weight: bold;">Search Criteria </span>
@For Each name As String In names
Dim prop As PropertyInfo = (From p In properties Where p.Name.ToLower = name Select p).FirstOrDefault
Dim value As Object = prop.GetValue(Model.Criteria)
If Not Nothing Is value Then
value = value.ToString
If Not String.IsNullOrEmpty(value) Then
@<span class="criteriaTag" title="click to remove" onclick="submitRemovalForm('@prop.Name');">@prop.Name<span class="criteriaValue">@value</span></span>
End If
End If
@Html.Hidden(prop.Name, value)
Next
</div>
End Using
<script type="text/javascript">
function submitRemovalForm(removeTag) {
$('#removalCriteria').val(removeTag);
$('#removalForm').submit();
}
</script>
那么,关于为什么会发生这种情况的任何想法?