2010-05-10 122 views
2

我刚刚开始使用asp.net mvc 2应用程序,我偶然发现了一个问题。我在更新我的表格时遇到问题。调试器不报告任何错误,它只是没有做任何事情......我希望有人能帮助我。感谢您的时间。 这是我的控制器代码...更新asp.net MVC 2应用程序中的数据的问题

public ActionResult Edit(int id) 
    { 
     var supplierToEdit = (from c in _entities.SupplierSet 
           where c.SupplierId == id 
           select c).FirstOrDefault(); 

     return View(supplierToEdit); 
    } 

    // 
    // POST: /Supplier/Edit/5 

    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Edit(Supplier supplierToEdit) 
    { 
     if (!ModelState.IsValid) 
      return View(); 

     try 
     { 
      var originalSupplier = (from c in _entities.SupplierSet 
            where c.SupplierId == supplierToEdit.SupplierId 
            select c).FirstOrDefault(); 

      _entities.ApplyPropertyChanges(originalSupplier.EntityKey.EntitySetName, supplierToEdit); 
      _entities.SaveChanges(); 
      // TODO: Add update logic here 

      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View(); 
     } 
    } 

这是我的看法......

<h2>Edit</h2> 

<% using (Html.BeginForm()) {%> 
    <%= Html.ValidationSummary(true) %> 

    <fieldset> 
     <legend>Fields</legend> 



     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.CompanyName) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.CompanyName) %> 
      <%= Html.ValidationMessageFor(model => model.CompanyName) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.ContactName) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.ContactName) %> 
      <%= Html.ValidationMessageFor(model => model.ContactName) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.ContactTitle) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.ContactTitle) %> 
      <%= Html.ValidationMessageFor(model => model.ContactTitle) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Address) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Address) %> 
      <%= Html.ValidationMessageFor(model => model.Address) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.City) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.City) %> 
      <%= Html.ValidationMessageFor(model => model.City) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.PostalCode) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.PostalCode) %> 
      <%= Html.ValidationMessageFor(model => model.PostalCode) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Country) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Country) %> 
      <%= Html.ValidationMessageFor(model => model.Country) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Telephone) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Telephone) %> 
      <%= Html.ValidationMessageFor(model => model.Telephone) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Fax) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Fax) %> 
      <%= Html.ValidationMessageFor(model => model.Fax) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.HomePage) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.HomePage) %> 
      <%= Html.ValidationMessageFor(model => model.HomePage) %> 
     </div> 

     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 

<% } %> 

<div> 
    <%= Html.ActionLink("Back to List", "Index") %> 
</div> 

+0

NULL是什么? _entities或originalSupplier?您的SupplierId是否仍然具有正确的价值? – 2010-05-10 12:40:41

回答

0

猜你得到一个例外。尝试把Tryception放在Try catch语句中

try 
{ 
    var originalSupplier = (from c in _entities.SupplierSet 
          where c.SupplierId == supplierToEdit.SupplierId 
          select c).FirstOrDefault(); 

    _entities.ApplyPropertyChanges(originalSupplier.EntityKey.EntitySetName, supplierToEdit); 
    _entities.SaveChanges(); 
    // TODO: Add update logic here 

    return RedirectToAction("Index"); 
} 
catch(Exception ex) 
{ 
    throw ex; 
    return View(); 
} 

因为我认为它的某些事情是基础的。

+0

它引发了一种怀疑 - 未将对象引用设置为对象的实例。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。 异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。 源错误: 95行:catch(异常ex) 行96:{ 行97:throw ex; 第98行:return View(); 第99行:} p.s.我是新的与asp.net也 – Bojan 2010-05-10 11:01:51

0

我会冒一个猜测,并说您的_entities变量在您试图触发ApplyPropertyChanges方法时为空。

这将导致抛出一个

Object reference not set to an instance of an object

错误。

0

试试这个

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Edit(Supplier supplierToEdit) 
{ 
    if (!ModelState.IsValid) 
     return View(); 

    try 
    { 
     var originalSupplier = (from c in _entities.SupplierSet 
           where c.SupplierId == supplierToEdit.SupplierId 
           select c).FirstOrDefault(); 
    _entities.UpdateModel(originalSupplier); 
     _entities.ApplyPropertyChanges(originalSupplier.EntityKey.EntitySetName, supplierToEdit); 
     _entities.SaveChanges(); 
     // TODO: Add update logic here 

     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 
相关问题