1

With this code I have error:

Object reference not set to an instance of an object

<% 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
2
  • Does the error occur during binding or on postback? Commented Jul 20, 2010 at 11:12
  • 1
    you do need to follow Darin's suggestion about returning the model to the view after posting back the form Commented Jul 20, 2010 at 12:06

3 Answers 3

2

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's why I deleted my first post... the model is being passed :)
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.
1

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

Comments

0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.