0

I can't seem to get the edit function of my view to work..i have a page that lists, a page that shows specific detail and on that page, i should be able to edit the information of the form..PROBLEM: when i run the application it says:No parameterless constructor defined for this object. What am i doing wrong...?

In the Home Controller i have:

Edit Functions:

       [HttpGet]
    public ViewResult EditSchoolDetails(int id)
    {
        var institution = _educationRepository.GetInstititionById(id);

        var model = (Mapper.Map<Institution, InstitutionModel>(institution));

        return View(model);
        }

post [HttpPost]

public ActionResult EditSchoolDetails( InstitutionModel institutionModel, int id)
  {

       if (ModelState.IsValid)             {
 //_get from repository and add to instituion
           var institution = _educationRepository.GetInstititionById(institutionModel.Id);
       // Map from the view model back to the domain model
             var model = Mapper.Map<Institution, InstitutionModel>(institution);
           //UpdateModel(model);
       SaveChanges();
    return RedirectToAction("ViewSchoolDetails", new {institutionModel = institutionModel, id = id});

           }

return View(institutionModel);
  }

InstitutionModel

  public class InstitutionModel { 
      public InstitutionModel() { 
          NAABAccreditations = new List<AccreditationModel>(); 
      } 

      public int Id { get; set; } 
      public string Name { get; set; } 
      public bool IsNAAB { get { return NAABAccreditations.Any(); } }
      public string Website { get; set; }
      public AddressModel Address { get; set; }
      public IEnumerable<AccreditationModel> NAABAccreditations { get; set; } 
   }
4imble
  • 13,979
  • 15
  • 70
  • 125
user1179083
  • 79
  • 1
  • 5
  • 17

3 Answers3

2

Does the Institution class have a parameterless constructor? If not, that will be the problem. You are passing an InstitutionModel to the the edit view, so the post action should probably take an InstitutionModel too, then you can map back to the original Institution model:

public ActionResult EditSchoolDetails(int id, InstitutionModel institutionModel)
{
    if (ModelState.IsValid)
    {
        //add to database and save changes
        Institution institutionEntity = _educationRepository.GetInstititionById(institution.Id);
        // Map from the view model back to the domain model
        Mapper.Map<InstitutionModel, Institution>(institutionModel, institutionEntity);
        SaveChanges();
        return RedirectToAction("ViewSchoolDetails",);
    }

    return View(institutionModel);
}

Notice also how it returns the view model back to the view if the model state isn't valid, otherwise you will lose all your form values!

Here's a similar question too which might help: ASP.NET MVC: No parameterless constructor defined for this object

Community
  • 1
  • 1
Ian Routledge
  • 4,012
  • 1
  • 23
  • 28
  • Mapper.Map(institutionModel, institutionEntity); is giving an error of mappig... – user1179083 Aug 14 '12 at 15:12
  • I tried to fix it to ``var model = Mapper.Map(institutionEntity);'```` but it returns this error: Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewSchoolDetails(Int32)' in 'NCARB.Web.Areas.Admin.Controllers.ToolsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters – user1179083 Aug 14 '12 at 15:17
  • @Iam Routledge: does it make sense how am explaining? – user1179083 Aug 14 '12 at 15:32
  • He's using AutoMapper in his example for the Mapper.Map (http://automapper.org/). Could be Moq as well I *think*. – Gromer Aug 14 '12 at 15:43
  • That exception (null parameter id) looks like its calling the HttpGet action, did you add the [HttpPost] attribute to the method I described above, I realised I left it off, sorry.Also, your first comment mentions you got a mapping error - you'd need to define this new mapping from InstitutionModel back to Institution, it won't automatically know how to reverse this mapping. – Ian Routledge Aug 15 '12 at 09:03
  • Oh no, I see it, its because your're redirecting to ViewSchoolDetails, but not passing the id of the entity you want to view (so the id is null), so that line should be return RedirectToAction("ViewSchoolDetails", new { id = institutionEntity.Id }); – Ian Routledge Aug 15 '12 at 09:06
0

Is it possible you need to pass a parameter to ViewSchoolDetails? I notice in the return statement you commented out that you were passing it an id, but in the return statement you're using, you're not passing in anything.

EDIT

This (from your comment below):

parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewSchoolDetails(Int32)

...tells me you need to pass a parameter to ViewSchoolDetails

EDIT 2

I saw your edit, and would say this: if the method you are calling is

public ActionResult ViewSchoolDetails(InstitutionModel institutionModel, int id) 

Then you MUST pass it an object of type InstitutionModel and an int as parameters or you will get an exception. Meaning, you need

RedirectToAction("ViewSchoolDetails", new {institutionModel = institutionModel, id = id});
Forty-Two
  • 7,535
  • 2
  • 37
  • 54
  • the viewschool details works because am returning the model that i've mapped it to – user1179083 Aug 14 '12 at 15:13
  • I have this now: ` [HttpPost] public ActionResult EditSchoolDetails(int? id, InstitutionModel institutionModel) { if (ModelState.IsValid) { var institution = _educationRepository.GetInstititionById(institutionModel.Id); // Map from the view model back to the domain model var model = Mapper.Map(institution); SaveChanges(); return RedirectToAction("ViewSchoolDetails"); } return View(institutionModel); } – user1179083 Aug 14 '12 at 15:37
  • and the error is : Server Error in '/' Application. -------------------------------------------------------------------------------- The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewSchoolDetails(Int32)' in 'NCARB.Web.Areas.Admin.Controllers.ToolsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters – user1179083 Aug 14 '12 at 15:37
  • i've added this: public ActionResult ViewSchoolDetails(int? id) { var institution = _educationRepository.GetInstititionById(id); var model = Mapper.Map(institution); return View(model); – user1179083 Aug 14 '12 at 15:53
  • didn't run at all with that change – user1179083 Aug 14 '12 at 16:00
  • Your missing the point of the error message. You need to pass a parameter to your method, not redefine the parameter as a nullable type. – Forty-Two Aug 14 '12 at 16:01
  • I saw your edit to my answer and elaborated a bit. Your problem is not the ActionMethod, but how you are calling it. – Forty-Two Aug 14 '12 at 16:24
  • works to go to next page but my values when edited don't change to the new values...look at my updated code above – user1179083 Aug 14 '12 at 16:25
0

Whenever i get this, i have forgotten to create a parameter-less constructor on my view-model. I always add one now just in case it's needed and i forget.

Does InstitutionModel have one?

4imble
  • 13,979
  • 15
  • 70
  • 125
  • institutionModel.cs/ class has: `public class InstitutionModel { public InstitutionModel() { NAABAccreditations = new List(); } public int Id { get; set; } public string Name { get; set; } public bool IsNAAB { get { return NAABAccreditations.Any(); } } public string Website { get; set; } public AddressModel Address { get; set; } public IEnumerable NAABAccreditations { get; set; } } } – user1179083 Aug 14 '12 at 16:30
  • What about AddressModel then? or AccreditationModel. Do all your viewmodels have one? – 4imble Aug 14 '12 at 16:36