我需要在asp.net MVC 4中应用更新和删除

卡玛尔·辛格(Kamal Singh)

基本上我已经创建了一个mvc应用程序,它将使用从数据库edmx生成的实体框架对数据库数据执行CRUD操作。因此在此之后,在添加EDM并配置数据库设置之后,我添加了一个名为CRUD的控制器,并启用了可读写的支架动作视图和附加的模型以及数据库上下文。现在一切都可以按需要进行,直到添加新员工即创建为止。问题出在我运行crud控制器的索引页面时,以及当我按下actionlink编辑或删除时任何特定的员工数据,它根本找不到编辑和删除cshtml页面,而是显示未找到错误404资源。请帮助我找出问题所在,我在下面发布了我的整个代码:

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")
}

解决方案资源管理器的图像: 解决方案资源管理器

阿里7091

在您的ActionLinks中传递EmployeeID:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我需要在asp.net mvc 4中重新布局布局视图的渲染主体部分

来自分类Dev

我需要在 ASP.Net Core + React 应用程序中使用 MVC 吗?

来自分类Dev

为什么我们需要在ASP.NET MVC中调用父类的空虚方法

来自分类Dev

我需要在 bundleconfig asp.net mvc 中添加所有的 javascript 和 css 链接吗?

来自分类Dev

ASP.Net MVC 4我需要延迟执行

来自分类Dev

DbExpressionBinding需要在ASP.NET MVC中具有集合ResultType的输入表达式

来自分类Dev

只需要在主页上的页脚中呈现表单。ASP.NET MVC

来自分类Dev

我们需要在Asp.net中为网站页面创建Class文件吗?

来自分类Dev

为什么我们不再需要在更高版本的ASP.NET Core中手动验证模型?

来自分类Dev

我需要在ASP.Net核心Web API的验证属性中返回自定义的验证结果(响应)

来自分类Dev

需要在ASP.NET MVC 5解决方案中创建代码以接收和发送JSON字符串服务器到服务器

来自分类Dev

Asp.Net MVC网站,我需要什么?

来自分类Dev

试图了解ASP.NET MVC中应用程序池刷新和视图的预编译的更详细信息

来自分类Dev

为什么在ASP.NET MVC 4 Web应用程序中未调用我的操作?

来自分类Dev

为什么在ASP.NET MVC 4 Web应用程序中未调用我的操作?

来自分类Dev

在ASP.NET MVC中需要帮助Chart.js

来自分类Dev

ImageMagic是否需要Ghostscript在ASP.NET MVC中运行?

来自分类Dev

更新ASP .Net MVC中的记录

来自分类Dev

更新ASP .Net MVC中的记录

来自分类Dev

我是否需要将AntiForgeryToken添加到ASP.NET MVC中的每个POST请求中以使其安全

来自分类Dev

我需要一个没有 EF 的 asp.net mvc 中带有复选框的下拉列表

来自分类Dev

ASP.NET MVC 4中的ViewComponent

来自分类Dev

ASP.NET MVC 4中的Treeview

来自分类Dev

ASP.NET MVC 4中的RegisterRoutes

来自分类Dev

具有Kendo UI网格的ASP.NET MVC无法创建更新和销毁

来自分类Dev

我如何在ASP.NET MVC中隐藏div

来自分类Dev

我在asp.net mvc 5中查看错误

来自分类Dev

我在asp.net mvc中的视图崩溃了吗?

来自分类Dev

在ASP.NET MVC4中-我需要编辑一条记录并向其中添加许多(1对多)子记录(在顶层记录的“编辑”模式下)

Related 相关文章

  1. 1

    我需要在asp.net mvc 4中重新布局布局视图的渲染主体部分

  2. 2

    我需要在 ASP.Net Core + React 应用程序中使用 MVC 吗?

  3. 3

    为什么我们需要在ASP.NET MVC中调用父类的空虚方法

  4. 4

    我需要在 bundleconfig asp.net mvc 中添加所有的 javascript 和 css 链接吗?

  5. 5

    ASP.Net MVC 4我需要延迟执行

  6. 6

    DbExpressionBinding需要在ASP.NET MVC中具有集合ResultType的输入表达式

  7. 7

    只需要在主页上的页脚中呈现表单。ASP.NET MVC

  8. 8

    我们需要在Asp.net中为网站页面创建Class文件吗?

  9. 9

    为什么我们不再需要在更高版本的ASP.NET Core中手动验证模型?

  10. 10

    我需要在ASP.Net核心Web API的验证属性中返回自定义的验证结果(响应)

  11. 11

    需要在ASP.NET MVC 5解决方案中创建代码以接收和发送JSON字符串服务器到服务器

  12. 12

    Asp.Net MVC网站,我需要什么?

  13. 13

    试图了解ASP.NET MVC中应用程序池刷新和视图的预编译的更详细信息

  14. 14

    为什么在ASP.NET MVC 4 Web应用程序中未调用我的操作?

  15. 15

    为什么在ASP.NET MVC 4 Web应用程序中未调用我的操作?

  16. 16

    在ASP.NET MVC中需要帮助Chart.js

  17. 17

    ImageMagic是否需要Ghostscript在ASP.NET MVC中运行?

  18. 18

    更新ASP .Net MVC中的记录

  19. 19

    更新ASP .Net MVC中的记录

  20. 20

    我是否需要将AntiForgeryToken添加到ASP.NET MVC中的每个POST请求中以使其安全

  21. 21

    我需要一个没有 EF 的 asp.net mvc 中带有复选框的下拉列表

  22. 22

    ASP.NET MVC 4中的ViewComponent

  23. 23

    ASP.NET MVC 4中的Treeview

  24. 24

    ASP.NET MVC 4中的RegisterRoutes

  25. 25

    具有Kendo UI网格的ASP.NET MVC无法创建更新和销毁

  26. 26

    我如何在ASP.NET MVC中隐藏div

  27. 27

    我在asp.net mvc 5中查看错误

  28. 28

    我在asp.net mvc中的视图崩溃了吗?

  29. 29

    在ASP.NET MVC4中-我需要编辑一条记录并向其中添加许多(1对多)子记录(在顶层记录的“编辑”模式下)

热门标签

归档