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