簡體   English   中英

MVC:通過SelectListItem,我得到“ System.NullReferenceException:對象引用未設置為對象的實例。”錯誤

[英]MVC: By SelectListItem I get “System.NullReferenceException: Object reference not set to an instance of an object.” error

通過SelectListItem,我得到“ System.NullReferenceException:對象引用未設置為對象的實例。” 錯誤。 我怎么了

這是我的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>

這是我的模特

public class ArtiestInfoModel
    {
        [DisplayName("Selecteer artiest: ")]
        public int ID_Artiest { get; set; }
        public string SelectedArtiest { get; set; }
        public IEnumerable<SelectListItem> ArtiestenLijst { get; set; }
    }

控制器:

 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();
        }

下一個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>

謝謝

在您的控制器中,您將節點ID_artiest讀為小寫,而xml顯示駝峰式。

更改

Value = artiest.Attribute("ID_artiest").Value,

Value = artiest.Attribute("ID_Artiest").Value,

如果artiest.Attribute("ID_artiest")結果為NULL,則讀取屬性Value將拋出null-reference-exception。

請初始化為這樣的新對象,例如ArtiestenLijst = new ArtiestenLijst(); 然后使用后

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM