1
型號可空模型的HTML幫助的DropDownList引發的ArgumentException
namespace Models
{
public class PartialTestModel
{
public int? NullableProp { get; set; }
}
}
這工作得很好
<div class="highlight1">
@Html.DropDownListFor(m => m.NullableProp, new SelectList(Enumerable.Range(1, 10), Model.NullableProp), "-- Select one --")
</div>
然而,如果該物業的
@model System.Int32?
<div class="highlight1">
Model's value is
@if (@Model.HasValue)
{
@Model.ToString()
}
else
{
<text>Null</text>
}
@Html.DropDownListFor(m => m, new SelectList(Enumerable.Range(1, 10), Model), "-- Select one --")
</div>
創建局部視圖,則拋出錯誤
Value cannot be null or empty.
Parameter name: name
[ArgumentException: Value cannot be null or empty.
Parameter name: name]
System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes) +396232
System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes) +35
System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel) +14
觀呈現部分作爲
@Html.Partial("_PartialTest", Model.NullableProp, new ViewDataDictionary())
new ViewDataDictionary()
被添加爲每答案中asp.net mvc renderpartial with null model gets passed the wrong type。如果沒有這個不正確的模型,當屬性值爲空時,在局部視圖中可以看到。當屬性值不爲空時,可以使用
@Html.Partial("_PartialTest", Model.NullableProp)
但仍然會導致與上面粘貼相同的錯誤。