[英]MVC: By SelectListItem I get “System.NullReferenceException: Object reference not set to an instance of an object.” error
By SelectListItem I get "System.NullReferenceException: Object reference not set to an instance of an object." 通过SelectListItem,我得到“ System.NullReferenceException:对象引用未设置为对象的实例。” error.
错误。 What do I wrong?
我怎么了
This is my xml: 这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<Artiesten>
<Item ID_Artiest="509998" Artiest="-" Product="-" Land="" Plaats="" Website="" Genre="Onbekend" />
<Item ID_Artiest="970119" Artiest="(hed)p.e." Product="-" Land="" Plaats="" Website="" Genre="Onbekend" />
<Item ID_Artiest="970080" Artiest="(urusei) yatsura" Product="-" Land="" Plaats="" Website="" Genre="Noise" />
<Item ID_Artiest="1010302" Artiest="...and you will know us by the trail of dead" Product="-" Land="Verenigde Staten" Plaats="Austin (TX)" Website="" Genre="Noise" />
...
</Artiesten>
This is my Model 这是我的模特
public class ArtiestInfoModel
{
[DisplayName("Selecteer artiest: ")]
public int ID_Artiest { get; set; }
public string SelectedArtiest { get; set; }
public IEnumerable<SelectListItem> ArtiestenLijst { get; set; }
}
The Controller: 控制器:
public ActionResult Index(int? AID_Artiest=810000)
{
var document = XDocument.Load(Server.MapPath("~/App_Data/Artiesten.xml"));
var query = new ArtiestInfoModel
{
ArtiestenLijst =
(from artiest in document.Descendants("Artiesten").Elements("Item")
select new SelectListItem
{
Value = artiest.Attribute("ID_artiest").Value,
Text = artiest.Attribute("Artiest").Value,
Selected = false // artiest.Attribute("ID_artiest").Value == AID_Artiest.ToString()
}).ToList()
};
ViewBag.ID_Artiest = AID_Artiest.Value;
ViewBag.ArtiestenLijst = query.ArtiestenLijst;
return View();
}
The Controller is called by the next DropDownList: 下一个DropDownList调用Controller:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("Index", "Hitdossier", FormMethod.Post, new { id = "frmArtiest" })) %>
<% { %>
<table><tr><th width="1000" align="left">Selecteer artiest:</th></tr>
<tr>
<td><%= Html.DropDownList("ID_Artiest",
new SelectList(ViewBag.ArtiestenLijst, "Value", "Text"),
"-- Selecteer artiest --",
new
{
@onchange = "document.getElementById('frmArtiest').submit();"
})%>
</td>
</tr>
</table>
<br />
<br />
<div id="divPartialView">
<%= Html.Action("Detail_Hitdossier", new { AID_Artiest = ViewBag.ID_Artiest })%> <br />
</div>
<%} %>
</asp:Content>
thanks 谢谢
In your controller you are reading the node ID_artiest as a lowercase while the xml shows camelcase. 在您的控制器中,您将节点ID_artiest读为小写,而xml显示驼峰式。
Change 更改
Value = artiest.Attribute("ID_artiest").Value,
To 至
Value = artiest.Attribute("ID_Artiest").Value,
If artiest.Attribute("ID_artiest")
result in a NULL value, then reading a property Value will throw the null-reference-exception. 如果
artiest.Attribute("ID_artiest")
结果为NULL,则读取属性Value将抛出null-reference-exception。
Please initialise as new object like this ArtiestenLijst = new ArtiestenLijst();
请初始化为这样的新对象,例如
ArtiestenLijst = new ArtiestenLijst();
then after use 然后使用后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.