MVC 3编辑功能不起作用

时间:2012-08-14 14:45:42

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 viewmodel edit

我似乎无法使我的视图的编辑功能工作..我有一个列出的页面,一个显示特定细节的页面,在该页面上,我应该能够编辑表单的信息。 .PROBLEM:当我运行应用程序时,它说:没有为此对象定义无参数构造函数。我做错了什么......?

在家庭控制器中我有:

编辑功能:

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

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

        return View(model);
        }

交                              [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; } 
   }

3 个答案:

答案 0 :(得分:2)

Institution类是否有无参数构造函数?如果没有,那就是问题所在。您正在将InstitutionModel传递到修改视图,因此后期操作也可能需要InstitutionModel,然后您可以映射回原始的Institution模型:

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

另请注意,如果模型状态无效,它如何将视图模型返回到视图,否则您将丢失所有表单值!

这也是一个类似的问题,可能有所帮助:ASP.NET MVC: No parameterless constructor defined for this object

答案 1 :(得分:0)

您是否可能需要将参数传递给ViewSchoolDetails?我在return语句中注意到你注释掉了你传给它一个id,但是在你正在使用的return语句中,你没有传递任何东西。

修改

这(来自下面的评论):

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

...告诉我你需要将参数传递给ViewSchoolDetails

编辑2

我看到了你的编辑,并且会说:如果你正在调用的方法是

public ActionResult ViewSchoolDetails(InstitutionModel institutionModel, int id) 

然后你必须传递一个类型为InstitutionModelint的对象作为参数,否则你会得到一个例外。意思是,你需要

RedirectToAction("ViewSchoolDetails", new {institutionModel = institutionModel, id = id});

答案 2 :(得分:0)

每当我得到这个,我忘记在我的视图模型上创建一个无参数构造函数。我现在总是添加一个以防万一它需要我忘了。

InstitutionModel有一个吗?