views:

113

answers:

2

Hello everyone, i am just getting started with asp .NET MVC 2 applications and i stumbled upon a problem. I'm having trouble updating my tables. The debugger doesn't report any error, it just doesn't do anything... I hope some can help me out. Thank you for your time. This is my controller code...

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

This is my 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>

A: 

Guess you are getting an exception. Try to throw the exeception in the Try catch statement

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

Because i think its something underlaying.

Patrik Potocki
It trows an exeception - Object reference not set to an instance of an object.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:Line 95: catch(Exception ex)Line 96: {Line 97: throw ex;Line 98: return View();Line 99: }p.s. i'm new with asp .NET also
Bojan
A: 

I'll Hazard a guess and say that your _entities variable is null at the time you're trying to fire off the ApplyPropertyChanges method.

This would cause a

Object reference not set to an instance of an object

error to be thrown.

Jamie Dixon