Octav
Octav

Reputation: 21

Server error in / Application. The resource was not found

I created an ASP.NET MVC application, and i'm having trouble when I'm trying to edit an entry, after I create it; editing the values already in databse works fine, but when i try to edit an entry that i created, it gives me this error:

Server Error in '/' Application.

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Here are my controller Edit and Create action methods:

  public ActionResult Create()
        {           
            return View();
        } 

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(Customer cs, bool Ontario, bool IN, bool MA)
        {
            try
            {

                ViewData["Ontario"] = Ontario;
                ViewData["IN"] = IN;
                ViewData["MA"] = MA;

                northwndEntities nw = new northwndEntities();

                if (ViewData["Ontario"].Equals(true))
                {
                    cs.Region = "Ontario";
                }
                else
                    if (ViewData["IN"].Equals(true))
                        cs.Region = "Indianapolis";
                    else
                        if (ViewData["MA"].Equals(true))
                            cs.Region = "Massachussets";
                nw.Customers.AddObject(cs);
                nw.SaveChanges();


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

 public ActionResult Edit(string id)
        {

            return View(GetCustomer(id));
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(Customer cust, bool ontario, bool IN, bool MA)
        {
            try
            {               
                ViewData["Ontario"] = ontario;
                ViewData["IN"] = IN;
                ViewData["MA"] = MA;

                northwndEntities nw = new northwndEntities();

                if (ViewData["Ontario"].Equals(true))
                {
                    cust.Region = "Ontario";
                }
                else
                    if (ViewData["IN"].Equals(true))
                        cust.Region = "Indianapolis";
                    else
                        if (ViewData["MA"].Equals(true))
                            cust.Region = "Massachussets";

                Customer origCust = GetCustomer(cust.CustomerID);
                nw.Customers.Attach(cust);
                nw.ApplyOriginalValues("Customers", origCust);
                nw.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

 [NonAction]
        public Customer GetCustomer(string id)
        {
            northwndEntities nw = new northwndEntities();

            var cust = from c in nw.Customers
                       where c.CustomerID == id
                       select c;
            Customer customer = cust.FirstOrDefault();

            return customer;

        }

Upvotes: 0

Views: 2994

Answers (2)

To get rid of the %20 char for every missing char just add .Trim() to the route value in the view:

@Url.Action("Edit", new { id=item.TheIdValue.Trim() }

or

@Html.ActionLink("Edit", "Edit", new { id = item.TheIdValue.Trim() })

Upvotes: 0

Octav
Octav

Reputation: 21

Problem solved! In Global.asax, the routing is implemented like this:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new {
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional
    } // Parameter defaults
);

The problem was that the ID is defined in the database as nchar(5). When I created a new entry in the database, it should have had a CustomerID field of EXACTLY 5 chars. If not (less than 5 chars), the resulting URL would have a %20 char for every missing char in the CustomerID field, and thus generated the "Server Error...".

So, better use int IDs, you avoid unnecessary issues.

Upvotes: 2

Related Questions