2016-11-03 1 views
0

Fondamentalement, j'ai créé une application mvc qui effectuera des opérations CRUD sur les données de la base de données en utilisant l'infrastructure de base générée à partir de la base de données edmx.So après avoir ajouté EDM et configuration de base de données paramètres j'ai ajouté un contrôleur nommé CRUD avec échafaudage activé pour lire/écrire des vues d'action et le modèle joint et DB conttex à cela.Maintenant tout fonctionne comme souhaité jusqu'à l'ajout d'un nouvel employé, c'est à dire Créer.Le problème vient quand je lance l'index page de contrôleur de crud et dans lequel quand j'appuie sur le lien d'action modifier ou supprimer pour des données d'employés spécifiques, il ne trouve pas du tout Modifier et supprimer les pages cshtml du tout et montre à la place l'erreur 404 ressource introuvable.Veuillez m'aider à comprendre ce qui ne va pas , je poste mon code entier ci-dessous:J'ai besoin d'appliquer la mise à jour et supprimer dans asp.net MVC 4

public class CRUDController : Controller 
{ 
    private TestDBEntities2 db = new TestDBEntities2(); 
    public ActionResult Index() 
    { 
     return View(db.Emps.ToList()); 
    } 
    public ActionResult Details(int id = 0) 
    { 
     Emp emp = db.Emps.Find(id); 
     if (emp == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(emp); 
    } 
    public ActionResult Create() 
    { 
     return View(); 
    } 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(Emp emp) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Emps.Add(emp); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(emp); 
    } 
    public ActionResult Edit(int id = 0) 
    { 
     Emp emp = db.Emps.Find(id); 
     if (emp == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(emp); 
    } 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Edit(Emp emp) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Entry(emp).State = EntityState.Modified; 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(emp); 
    } 
    public ActionResult Delete(int id = 0) 
    { 
     Emp emp = db.Emps.Find(id); 
     if (emp == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(emp); 
    } 
    [HttpPost, ActionName("Delete")] 
    [ValidateAntiForgeryToken] 
    public ActionResult DeleteConfirmed(int id) 
    { 
     Emp emp = db.Emps.Find(id); 
     db.Emps.Remove(emp); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    protected override void Dispose(bool disposing) 
    { 
     db.Dispose(); 
     base.Dispose(disposing); 
    } 
} 

Index.cshtml

@model IEnumerable<MvcApplication2.Emp> 
@{ 
    ViewBag.Title = "Index"; 
} 
<h2>Index</h2> 
<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.EmployeeID) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Firstname) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Lastname) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Age) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Project) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Address) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.EmployeeID) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Firstname) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Lastname) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Age) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Project) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Address) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { /*id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) 
     </td> 
    </tr> 
} 

</table> 

Create.cshtml

@model MvcApplication2.Emp 
@{ 
    ViewBag.Title = "Create"; 
} 
<h2>Create</h2> 
@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Emp</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.EmployeeID) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.EmployeeID) 
      @Html.ValidationMessageFor(model => model.EmployeeID) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Firstname) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Firstname) 
      @Html.ValidationMessageFor(model => model.Firstname) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Lastname) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Lastname) 
      @Html.ValidationMessageFor(model => model.Lastname) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Age) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Age) 
      @Html.ValidationMessageFor(model => model.Age) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Project) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Project) 
      @Html.ValidationMessageFor(model => model.Project) 
     </div> 

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

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

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

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

Edit.cshtml

@model MvcApplication2.Emp 
@{ 
    ViewBag.Title = "Edit"; 
} 
<h2>Edit</h2> 
@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Emp</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.EmployeeID) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.EmployeeID) 
      @Html.ValidationMessageFor(model => model.EmployeeID) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Firstname) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Firstname) 
      @Html.ValidationMessageFor(model => model.Firstname) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Lastname) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Lastname) 
      @Html.ValidationMessageFor(model => model.Lastname) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Age) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Age) 
      @Html.ValidationMessageFor(model => model.Age) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Project) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Project) 
      @Html.ValidationMessageFor(model => model.Project) 
     </div> 

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

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

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

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

Image de l'Explorateur de solutions: Solution Explorer

+0

pourquoi avez-vous comment "id = item.PrimaryKey" dans ActionLinks? – Ali7091

+0

Je ne pense pas que vos liens d'action passent les paramètres corrects –

+2

vous devriez passer "id = item.EmployeeID" dans ActionLinks – Ali7091

Répondre

1

passe EmployeeID dans votre ActionLinks:

@Html.ActionLink("Edit", "Edit", new {id=item.EmployeeID }) | 
@Html.ActionLink("Details", "Details", new {id=item.EmployeeID }) | 
@Html.ActionLink("Delete", "Delete", new {id=item.EmployeeID }) 
0

essayer d'ajouter e e contrôleur Nom

@Html.ActionLink("Edit", "Edit", "CRUD", new {/*id=item.PrimaryKey */ }) | 
@Html.ActionLink("Details", "Details", "CRUD", new { /*id=item.PrimaryKey */ }) | 
@Html.ActionLink("Delete", "Delete", "CRUD", new { /*id=item.PrimaryKey */ }) 
+0

qui n'a pas fonctionné monsieur! –