简体   繁体   English

MVC:通过SelectListItem,我得到“ System.NullReferenceException:对象引用未设置为对象的实例。”错误

[英]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.

相关问题 System.NullReferenceException:“对象引用未设置为 object 的实例。” 问题 - System.NullReferenceException: „Object reference not set to an instance of an object.” problem System.NullReferenceException:对象引用未设置为对象的实例。 抛出错误 - System.NullReferenceException: Object reference not set to an instance of an object. throwing error 为什么在运行单元测试时出现以下错误:“ System.NullReferenceException:对象引用未设置为对象的实例。” - Why am I getting the following error: “System.NullReferenceException: Object reference not set to an instance of an object.” when I run a Unit Test? 错误:System.NullReferenceException:对象引用未设置为对象的实例 - Error :System.NullReferenceException: Object reference not set to an instance of an object 会话{“对象引用未设置为对象的实例。”} System.Exception {System.NullReferenceException} - session {“Object reference not set to an instance of an object.”} System.Exception {System.NullReferenceException} System.NullReferenceException:未将对象引用设置为对象的实例。 在测试自动化执行期间 - System.NullReferenceException: Object reference not set to an instance of an object. During Test automation execution System.NullReferenceException未将对象引用设置为对象的实例。 关于模型实例化 - 有时候 - System.NullReferenceException Object reference not set to an instance of an object. on Model instantiation - SOMETIMES “ System.NullReferenceException-对象引用未设置为对象的实例。” Web方法,用户控件 - “System.NullReferenceException— Object reference not set to an instance of an object.” Webmethod, User control 获取 System.NullReferenceException:“对象引用未设置为 object 的实例。” 调用服务时 - getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when calling a service System.NullReferenceException-对象引用未设置为对象的实例。 C#中的原因 - System.NullReferenceException - Object reference not set to an instance of an object. Causes in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM