gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 验证针对空列表框抛出 NullReferenceException

转载 作者:行者123 更新时间:2023-12-02 03:39:12 25 4
gpt4 key购买 nike

我有两个列表框,一个用于可用服务器,一个用于选定服务器。用户将项目从第一个框移动到第二个框以指示他们的选择。

<table id="ServerLists" style="width: 100%;">
<tr>
<td style="width: 40%;">
<%=Html.ListBox("AvailableServerList", Model.AvailableServerList, New With {.size = 8, .ondblclick = "addServer();"})%>
</td>
<td style="width: 20%;">
<button type="button" onclick="addAll();">Add All &gt;&gt;</button>
<button type="button" onclick="addServer();">Add &gt;</button>
<button type="button" onclick="removeServer();">&lt; Remove</button>
<button type="button" onclick="removeAll();">&lt;&lt; Remove All</button>
</td>
<td style="width: 40%;">
<%=Html.ListBox("SelectedServerList", Model.SelectedServerList, New With {.size = 8, .ondblclick = "removeServer();"})%>
</td>
</tr>
</table>

如果没有服务器添加到第二个列表框,我会向 ModelState 添加错误。

问题是,如果列表框中没有条目,则在渲染带有验证警告的 View 时,MVC 会抛出 NullReferenceException。

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType) +63
System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes) +155
System.Web.Mvc.Html.SelectExtensions.ListBox(HtmlHelper htmlHelper, String name, IEnumerable`1 selectList, Object htmlAttributes) +62
ASP.views_serveraccess_create_aspx.__RendercMain(HtmlTextWriter __w, Control parameterContainer) in C:\Documents and Settings\nfoster\My Documents\Visual Studio Projects\Client Portal 3\Trunk\src\Stargate3.Web\Views\ServerAccess\Create.aspx:148
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in C:\Documents and Settings\nfoster\My Documents\Visual Studio Projects\Client Portal 3\Trunk\src\Stargate3.Web\Views\Shared\Site.Master:97
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +57
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

还有其他人遇到过这个问题吗?有什么我可以尝试的解决方法吗?如果没有做出任何选择,我可能必须在其中插入一个[请选择服务器]条目,但如果能修复此问题,我们将不胜感激。

编辑:根据要求添加 Controller 操作

<AcceptVerbs(HttpVerbs.Post)> _
Function Create(ByVal collection As FormCollection) As ActionResult
Dim acceptedFields() As String = {"StartTime", "EndTime", "ReasonForAccess", "WorkToBeDone", "RegressionPlan", "Servers", "AccessAccount", "Password"}
Dim accessRequest As New ServerAccessRequest
UpdateModel(Of ServerAccessRequest)(accessRequest, acceptedFields)
If accessRequest.IsValid Then
'TODO: Do database stuff here
Return View("AccessRequestConfirmation")
Else
For Each violation In accessRequest.GetRuleViolations
ModelState.AddModelError(violation.PropertyName, violation.ErrorMessage)
Next
End If
Return View(New ServerAccessViewModel(accessRequest))
End Function

最佳答案

请参阅release notes适用于 ASP.NET MVC 2 预览版 1;显然,这个(将 null 传递到模型状态时出现 NullReferenceException)是 ASP.NET MVC 中的一个已知错误。

关于asp.net-mvc - ASP.NET MVC 验证针对空列表框抛出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1213659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com