Click here to Skip to main content
15,889,864 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
when i click on the Edit link , it displays...
C#
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.


When i remove,
C#
if (StuEdit == null) //it displays an empty view on the web browser
{
   return HttpNotFound();
}


What I have tried:

C#
using LinkViewStudentDb.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LinkViewStudentDb.Controllers
{
    public class HomrController : Controller
    {
        //
        // GET: /Homr/
        UserDb us = new UserDb();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ViewAllStu()
        {
            UserDb us = new UserDb();
            var stuCol = us.Students.Select(st => st);
            return View(stuCol);
        }
        public ActionResult create()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult create(FormCollection form)
        {
            var StuCre = new Student();

            TryUpdateModel(StuCre, form.ToValueProvider());


            if (ModelState.IsValid)
            {
                us.Students.Add(StuCre);
                us.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(StuCre);
        }


        public ActionResult edit(string id = null)
        {
            Student StuEdit = us.Students.Find(id);
       
            if (StuEdit == null)
            {
                return HttpNotFound();
            }
            return View(StuEdit);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult edit([Bind(Include = "StuID, name, Surname, age,sex")]Student StuEdit)
        {
            TryUpdateModel(StuEdit);
            if (ModelState.IsValid)
            {

                us.Entry(StuEdit).State = EntityState.Modified;
                us.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(StuEdit);

        }
    }
}
Posted
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900