system.nullreferenceException {"对象引用未设置为对象的实例。“} [英] system.nullreferenceException {"Object reference not set to an instance of an object."}

查看:93
本文介绍了system.nullreferenceException {"对象引用未设置为对象的实例。“}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试去编辑表单时的例外:



system.nullreferenceException {对象引用未设置为对象的实例。}











我的控制器:





  public  ActionResult Edit ( int  id)
{
var ModifierUser =(来自 u _db.Utilisateurs
其中​​ u。 IdUser == id
选择 u).First();
return 查看(ModifierUser);
}

//
// POST:/ Users / Edit / 5

[AcceptVerbs(HttpVerbs.Post) ]
public ActionResult Edit(Utilisateurs ModifierUser)
{

var OriginalUser =( from u in _db.Utilisateurs
where u.IdUser == ModifierUser.IdUser
select u).FirstOrDefault();

if (!ModelState.IsValid)
{
return 查看(OriginalUser);
}
_db.ApplyCurrentValues(OriginalUser.EntityKey.EntitySetName,ModifierUser);
_db.SaveChanges();

return RedirectToAction( LISTUSER);

}



视图:



&

 lt;%foreach(模型中的var项目){%> 
< tr >

< td style = width:78px; height :32px; >

<%使用(Html.BeginForm( 编辑 用户 new {id = item.IdUser}))
{%> ;
< 输入 < span class =code-attribute> type = image src = ../../ Image / edit-validated.png alt = 编辑

style = 身高:18px / >
<% }%> < / td > < / tr >

解决方案

loymed,



你遇到的问题很微妙。回发编辑重载无法将表单数据反序列化回 ModifierUser 作为绑定到项目在一个列表中,该列表是 Utilisateurs 本身的子属性。具体来说, new {id = item.IdUser} 不会设置 ModifierUSer.IdUser

IIRC如果您更改

使用(Html.BeginForm(编辑,用户,新{id = item.IdUser}))



使用(Html.BeginForm(编辑,用户,新{ IdUser  = item.IdUser}))



这将填充该值,但我可能会误解我所做的事情(通常我会检查但是我在工作)。此外,如果您要更新此值(并且看起来很像),那么您将难以区分旧的新用户ID。



还有其他几种方法可以保留旧/新id(与目前相同的RAZOR):





 [AcceptVerbs(HttpVerbs.Post)] 
public ActionResult编辑( int id, / * ...其他您需要从表单中获取的值 - 名称必须与* / 匹配)
{
var OriginalUser =(来自 u _db.Utilisateurs
其中​​ u.IdUser == id
选择 u).FirstOrDefault();
/ * 以前的其余代码* /
}





或者还有一个你可以使用的表格系列可能更整洁,取决于你还在做什么。



 [AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit(FormsCollection formsCollection)
{
var OriginalUser =( from u in _db.Utilisateurs
其中​​ u.IdUser == formsCollection [ id]
选择 u).FirstOrDefault();
/ * 以前的其余代码* /
}


this is the exception when i try to go to edit form:

system.nullreferenceException {"Object reference not set to an instance of an object."}





my controller:


public ActionResult Edit(int id)
        {
            var ModifierUser = (from u in _db.Utilisateurs
                                where u.IdUser == id
                                select u).First();
            return View(ModifierUser);
        }

        //
        // POST: /Users/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Utilisateurs ModifierUser)
        {
           
            var OriginalUser = (from u in _db.Utilisateurs
                                where u.IdUser == ModifierUser.IdUser
                                select u).FirstOrDefault();

            if (!ModelState.IsValid)
            {
                return View(OriginalUser);
            }
            _db.ApplyCurrentValues(OriginalUser.EntityKey.EntitySetName,ModifierUser);
            _db.SaveChanges();

            return RedirectToAction("ListUser");

        }


the view:

&

lt;% foreach (var item in Model) { %>
    <tr>
      
        <td style="width: 78px; height: 32px;">
        
        <% using(Html.BeginForm("Edit","Users",new{id=item.IdUser}))
           { %>
           <input type="image" src="../../Image/edit-validated.png"  alt="Edit" 

                 style="height: 18px" />
                 <%} %></td></tr>

解决方案

Hi loymed,

The problem you are having is subtle. The postback Edit overload won''t be able to de-serialise the form data back into ModifierUser as binds to items in a list which is a subproperty of Utilisateurs itself. Specifically the new{id=item.IdUser} won''t set ModifierUSer.IdUser.
IIRC if you change

using(Html.BeginForm("Edit","Users",new{id=item.IdUser}))

to

using(Html.BeginForm("Edit","Users",new{IdUser=item.IdUser}))


this will populate the value, but I could be mis-remembering what I did (normally I''d check but I''m at work). Additionally, if you are updating this value (and it looks like you are) might be, you are going to have problems distinguishing between the old an new user ids.

There are a few other ways to skin this preserving the old/new id (this same RAZOR as you have currently):


[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, /*... other values you need from the form - names must match*/)
{
var OriginalUser = (from u in _db.Utilisateurs
                                where u.IdUser == id
                                select u).FirstOrDefault();
/* Rest of code as before*/
}



or there is also a forms collection you can use which might be neater, depends on what else you are doing.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(FormsCollection formsCollection)
{
var OriginalUser = (from u in _db.Utilisateurs
                                where u.IdUser == formsCollection["id"]
                                select u).FirstOrDefault();
/* Rest of code as before*/
}


这篇关于system.nullreferenceException {&quot;对象引用未设置为对象的实例。“}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆