tags:

views:

64

answers:

3

With this code I have error: Object reference not set to an instance of an object, why

<% using (Html.BeginForm("XMLDevicesAddFirmware","ImportXML",FormMethod.Post)) {%>

<table class="data-table">
    <tr>

        <th>
            Article Number
        </th>
        <th>
            Firmware
        </th>

        <th>
            Name
        </th>
        <th>
            Order Id
        </th>

        <th>
            Software Version
        </th>
    </tr>

<% int rb = 1;%>

<% foreach (var item in Model) { %>

    <tr>

        <td>
            <%= Html.Encode(item.ArticleNumber) %>
        </td>

        <td>
            <input id="Firmware" name="<%= Html.Encode(rb)%>" type="text" />
        </td>

        <td>
            <%= Html.Encode(item.Name) %>
        </td>
        <td>
            <%= Html.Encode(item.OrderId) %>
        </td>

        <td>
            <input id="SoftwareVersion" name="<%= Html.Encode(rb)%>" type="text" />
        </td>
    </tr>
   <% rb = rb + 1;%>
   <% } %>

</table>

        <p>
            <input type="submit" value="Finish" />
        </p>

 <% } %>



    public ActionResult XMLDevicesAddFirmware()
    {

        var dev = from i in XMLEntities.unassigneditems
                  where i.DevOrAcc == true
                  select i;

        return View(dev);
    }



    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult XMLDevicesAddFirmware(FormCollection col)
    {
        //....
        return View();
    }

Stack Trace:

 [NullReferenceException: Object reference not set to an instance of an object.]
 ASP.views_importxml_xmldevicesaddfirmware_aspx.__RenderContent2(HtmlTextWriter __w,     Control parameterContainer) in c:\Documents and Settings\Ognjen\My Documents\Visual Studio 2008\Projects\MvcKVteam - radna verzija_18_07\MvcKVteam - radna verzija\MvcKVteam\Views\ImportXML\XMLDevicesAddFirmware.aspx:36
 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
 ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Documents and Settings\Ognjen\My Documents\Visual Studio 2008\Projects\MvcKVteam - radna verzija_18_07\MvcKVteam - radna verzija\MvcKVteam\Views\Shared\Site.Master:104
 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.Page.Render(HtmlTextWriter writer) +29
 System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
 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
A: 

Is the method public ActionResult XMLDevicesAddFirmware(string Firmware, string SoftwareVersion) being hit. I can't see that your form is posting back to this method, I mean, there is no string Firmware, string SoftwareVersion in the route.

try this

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult XMLDevicesAddFirmware(FormCollection coll)
    {
        //....
        return View();
    }

edit: or on the Html.BeginForm add new { Firmware = Model.Firmware, SoftwareVersion = Model.SoftwareVersion}

Simon Hazelton
+1  A: 

Don't forget to pass the model to the view:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult XMLDevicesAddFirmware(string Firmware, string SoftwareVersion)
{
    var model = new SomeModel();
    return View(model);
}
Darin Dimitrov
That's why I deleted my first post... the model is being passed :)
Simon Hazelton
From what I can see you are not passing the model inside the POST action. You are passing it only in the GET action which of course is not sufficient as both render the same view that **requires** the model.
Darin Dimitrov
A: 

If the model is not valid and you are unable to do execute some of your code in the

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult XMLDevicesAddFirmware(FormCollection col)
{
    //....
    return View();
}

you need to return same model to the View()

return View(dev);

like u did with the get action,

otherwise the view cannot display because model is null.

when u do return View(dev); view will render fine, and if u did ModelState.AddModelError(/**/); the view will show validation errors

Nikola Markezic