1

There are already a normal scaffolding for CRUD in Asp.net MVC but i am using my own code for saving the post.

Abstract.cs(Model)

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

This is my controller and model. Here i am trying to implement my own way to perform edit and update stuffs. Here i am really confused how to perform edit stuffs for this create action.

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

But this isn't working please suggest or provide me a solution to write a edit action.

5
  • Why are you not using AbstractFormViewModel for your Edit() method? Commented Mar 23, 2017 at 9:19
  • And what is not working? (and I assume db.Entry(Abstract). is a typo and is really db.Entry(abstract). - ditto in return View in both methods. Commented Mar 23, 2017 at 9:22
  • Put your code in the question, not comments (its impossible to read especially when you don't format it) Commented Mar 23, 2017 at 9:25
  • Seems that db.Entry(Abstract).State = EntityState.Modified possibly throwing error during entity modification. You can use AbstractFormViewModel in Edit method to fill Abstract model before using EntityState.Modified. Commented Mar 23, 2017 at 9:25
  • In the GET method, initialize a new AbstractFormViewModel() and set its properties based on the data model and pass that view model to the view (its just the opposite if what you do in the Create POST method Commented Mar 23, 2017 at 9:36

1 Answer 1

2

Try this , Put your varibales and model in it

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()); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Server Error in '/' Application. The model item passed into the dictionary is of type 'shanuMVCUserRoles.Models.Abstract', but this dictionary requires a model item of type 'shanuMVCUserRoles.ViewModels.AbstractFormViewModel'. Throwing Error
Are you passing direct Model , Here it is IEnumerable List , check again in your code what data you are passing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.