如何在ASP.NET MVC 5中执行编辑操作?

时间:2017-03-23 09:16:59

标签: c# asp.net asp.net-mvc asp.net-mvc-4

在Asp.net MVC中已有一个普通的CRUD脚手架,但我使用自己的代码来保存帖子。

Abstract.cs(型号)

public class Abstract
{
    public int ID { get; set; }

    [Required]
    public ApplicationUser Member { get; set; }

    [Required]
    public string AbstractTitle { get; set; }

    [Required]
    public string AbstractAim { get; set; }

    [Required]
    [StringLength(300)]
    public string AbstractDetails { get; set; }

    [Required]
    public string Author { get; set; }

    public string CoAuthor { get; set; }

}

AbstractController.cs

public class AbstractsController : Controller
{
    private readonly ApplicationDbContext _context;

    public AbstractsController()
    {
        _context = new ApplicationDbContext();
    }

    [Authorize]
    public ActionResult Create()
    {
        return View();
    }

    [Authorize]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(AbstractFormViewModel viewModel)
    {
        if (!ModelState.IsValid)
            return View("Create", viewModel);

        var memberId = User.Identity.GetUserId();
        var member = _context.Users.Single(u => u.Id == memberId);
        var abs = new Abstract
        {
            Member = member,
            AbstractTitle = viewModel.AbstractTitle,
            AbstractAim = viewModel.AbstractAim,
            AbstractDetails = viewModel.AbstractDetails,
            Author = viewModel.Author,
            CoAuthor = viewModel.CoAuthor

        };
        _context.Abstracts.Add(abs);
        _context.SaveChanges();
        return RedirectToAction("Index", "Home");
    }

}

这是我的控制器和型号。在这里,我试图实现自己的方式来执行编辑和更新的东西。 在这里,我真的很困惑如何为这个创建动作执行编辑。

    public ActionResult Edit(int id)
    {
        var customer = _context.Abstracts.SingleOrDefault(c => c.Id == id);

        if (customer == null)
            return HttpNotFound();
        var memberId = User.Identity.GetUserId();
        var member = _context.Users.Single(u => u.Id == memberId);
        var viewModel = new Abstract
        {
            Member = member,
            AbstractTitle = viewModel.AbstractTitle,
            AbstractAim = viewModel.AbstractAim,
            AbstractDetails = viewModel.AbstractDetails,
            Author = viewModel.Author,
            CoAuthor = viewModel.CoAuthor

        };

        return View("CustomerForm", viewModel);
    }

但这不起作用请建议或提供编写动作的解决方案。

1 个答案:

答案 0 :(得分:1)

试试这个,将变量和模型放入其中

public ActionResult Edit(string id, EMP objmodel)
    {
        if (System.Web.HttpContext.Current.Request.HttpMethod == "GET")
        {
            int j = Convert.ToInt32(id);

            EMP e =(EMP) db.EMPs.Single(n => n.id == j);
            //Either use Single(), SingleorDefault(), First(), FirstorDefault()
            return View(e);

        }
        else if (System.Web.HttpContext.Current.Request.HttpMethod == "POST")
        {
            if (objmodel.Name != null && objmodel.Email != null && objmodel.DOB != null)
            {
                //db.EMPs.AddorUpdate(objmodel);//requires using System.Data.Entity.Migrations;
                db.EMPs.Attach(objmodel);
                db.Entry(objmodel).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                ViewBag.alert = u1.AlertDismissable("alert-success", "Record Updated successfully");
                TempData["alert"] = u1.AlertDismissable("alert-success", "Record Updated successfully");
                //return View("ShowAll", db.EMPs.ToList());
                return RedirectToAction("index", db.EMPs.ToList());
            }
            else
            {
                ViewBag.alert = u1.AlertDismissable("alert-danger", "No record Gets Updated , Maybe some fields are Empty");
                TempData["alert"] = u1.AlertDismissable("alert-danger", "No record Gets Updated , Maybe some fields are Empty");
                return View();
            }
        }

        ViewBag.alert = u1.AlertDismissable("alert-success", "Record added successfully");
        TempData["alert"] = u1.AlertDismissable("alert-success", "Record added successfully");
        //return View("ShowAll", db.EMPs.ToList());
        return RedirectToAction("index", db.EMPs.ToList());
    }