CRUD,在同一视图上创建和更新功能:ASP.net MVC2 + EF

时间:2014-10-29 05:33:35

标签: asp.net-mvc entity-framework

我是一名新手,正在使用MVC2 + Entity框架在Visual Studio 2010中制作Web应用程序。

我有一种情况,我想把两个操作,即在同一视图中创建用户/更新用户,我也尝试附加相关图片,其中我为创建用户创建了两个部分,为管理用户创建了第二部分。

我的“创建用户”字段位于网站顶部,当用户点击“创建按钮”页面时,所有登记的用户都会在第二部分“管理用户”下显示相同视图,并显示编辑/删除链接。< / p>

我希望当我点击编辑链接时,该特定实体字段会在第一部分“创建用户”中的相同视图中填充,我可以在其中修改它们并按“更新按钮”

查看

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MyNamespace"  %>

<h4>Create New User</h4>
<form method="post" action="/Lignum/CUser">
   <label for="inputEmail3">Full Name</label>
   <input type="text" name="Fullname" id="txtFullname" >
   <label for="inputEmail3">Email</label>
   <input type="email" name="Email" id="Email1">
   <button id="btnCUser" class="btn btn-primary">Create</button>
</form>

<h4>Manage Users</h4>
<table>
  <tr>
     <td>Sr#</td><td>Name</td><td>Email</td><td></td>
  </tr>                 
<% int i=0;
foreach (MyWebsite.Models.User objUser in ViewData.Model as IEnumerable<MyWebsite.Models.User>)
{%>
  <tr>
     <td><%= ++i%></td>
     <td><%= objUser.Fullname%></td>
     <td><%= objUser.Email%></td>
     <td>
         <a href="/Controller/Edit/<%= objUser.UserId %>">Edit</a>
     </td>
  </tr>                             
<%}%>
</table>

CONTROLLER

public ActionResult Index()
{
    return View("UserMgt", _repositoryUser.SelectAll());
}

public ActionResult Edit(object Id)
{
    if (Id != null && Id.ToString().Trim().Length > 0)
    {
        int param = int.Parse(Id.ToString());
        return View("UserMgt", _repositoryUser.SelectByID(Id));
    }
    return View("404");
}

2 个答案:

答案 0 :(得分:2)

你需要使用JQuery&amp; Ajax实现这一目标。您的页面最有可能因为您提交表单而得到刷新。您需要附加一个函数来处理onclick事件,而不是表单提交。

在该功能中,您将知道单击了哪个项目,从发送带有项目ID的ajax请求的服务器加载要编辑的数据。

当请求返回时,您可以打开JQuery弹出窗口或更新页面的html以显示数据。用户将被允许进行更改,单击“确定”按钮,您可以再次将数据发送回服务器进行保存。

我正在寻找一个在线示例,因为我的代码有点复杂。您还可以在线查找示例。

<强>更新

  

我希望当我点击编辑链接时,该特定实体字段   在我可以的第一部分'创建用户'的同一视图中填充   修改它们并按“更新按钮”

好的,看看你的代码。正如我之前所说,你需要为每个html元素定义一个“id”,值为objUser.UserId(你可以根据需要添加前缀)。现在为所有html元素定义一个click事件,即。

有关工作示例,请参阅此link

我建议您在获得一些见解并发布更新代码时取得进展。我们将建议下一步所需的内容。通过这种方式,您可以了解更多信息。

答案 1 :(得分:1)

您可以尝试以下操作:

  • 使用创建页面上所需的任何内容创建一个View模型,即用户详细信息
  • 使用此视图模型强烈绑定您的视图
  • 在控制器中有三种操作方法&#34;创建&#34;,&#34;填充&#34;和&#34;更新&#34;使用Update和Populate将实体的Id作为输入(您可以选择更好的名称)
  • 最初调用Create方法,它将返回一个带有View
  • 的空视图模型
  • 在视图中有一个隐藏变量,它将存储实体的Id(如果创建,则为零)
  • 点击“创建”只需获取此隐藏变量的值并执行更新操作方法的帖子。在这种情况下,如果是新的实体ID将为零
  • 点击编辑后,调用具有实体ID的Populate方法,该实体将再次返回ViewModel,并将实体详细信息加载到同一创建视图中(同时使用id设置隐藏变量)
  • 在基于id的update方法中执行创建或更新操作,即Create for zero和Update for 1

如果您发布代码或其他详细信息,我可以使用代码提供更多详细信息。

编辑:在代码方面确定更多细节。

//This is the view model you need to bind to your view
   public class UserViewModel
{
    public int UserId { get; set; }
    public string Email { get; set; }
    public string FullName { get; set; }
    public List<Users> UserList {get;set;} //For binding to the grid
}

以下是控制器中的操作方法。

public ActionResult Create()
    {

        var viewModel = new UserViewModel();
        //Logic: Create empty view model for create
        return View("UserMgt", viewModel);
    }

    public ActionResult Edit(int id)
    {
        var viewModel = new UserViewModel();
        //Logic: populate the view model based on the id
        return View("UserMgt", viewModel);
    }
    // Call this method using Jquery ajax
    public bool Update(UserViewModel user)
    {
        if (user.Id == 0)
            //Logic : Create the user
        else
        //Logic : Edit the user

        return Json(status); //Status = true if successful else false
    }

最初调用create.On点击编辑调用编辑方法。点击保存调用更新。 有关使用jquery ajax,请点击以下链接

http://api.jquery.com/jquery.ajax/