你调用的对象是空的

时间:2010-07-20 11:04:28

标签: asp.net-mvc

使用此代码我有错误:

  

对象引用未设置为对象的实例

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

堆栈追踪:

 [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

3 个答案:

答案 0 :(得分:2)

不要忘记将模型传递给视图:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult XMLDevicesAddFirmware(string Firmware, string SoftwareVersion)
{
    var model = new SomeModel();
    return View(model);
}

答案 1 :(得分:1)

如果模型无效且您无法在

中执行某些代码
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult XMLDevicesAddFirmware(FormCollection col)
{
    //....
    return View();
}

您需要将相同的模型返回到View()

return View(dev);

就像你对get动作一样,

否则视图无法显示,因为模型为空。

当你return View(dev);视图呈现正常时,如果你ModelState.AddModelError(/**/);视图将显示验证错误

答案 2 :(得分:0)

方法是公共ActionResult XMLDevicesAddFirmware(字符串Firmware,string SoftwareVersion)被命中。我无法看到你的表单回复到这个方法,我的意思是,路由中没有字符串Firmware,字符串SoftwareVersion。

试试这个

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

编辑:或者在Html.BeginForm上添加新的{Firmware = Model.Firmware,SoftwareVersion = Model.SoftwareVersion}