【问题标题】:MVC 3 Edit Function not workingMVC 3编辑功能不起作用
【发布时间】:2012-08-10 21:32:47
【问题描述】:

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

在我的家庭控制器中:

编辑函数:

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

机构模型

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

【问题讨论】:

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


    【解决方案1】:

    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

    【讨论】:

    • Mapper.Map(机构模型,机构实体);给出了 mappig 的错误...
    • 我试图将其修复为 ``var model = Mapper.Map(institutionEntity);'```` 但它返回此错误:异常详细信息:System.ArgumentException:参数字典包含“NCARB.Web.Areas.Admin.Controllers.ToolsController”中方法“System.Web.Mvc.ActionResult ViewSchoolDetails(Int32)”的不可空类型“System.Int32”的参数“id”的空条目.可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数
    • @Iam Routledge:我的解释有意义吗?
    • 他在 Mapper.Map (automapper.org) 的示例中使用 AutoMapper。我认为也可能是最小起订量。
    • 那个异常(空参数 id)看起来像是调用了 HttpGet 操作,你是否将 [HttpPost] 属性添加到我上面描述的方法中,我意识到我把它遗漏了,对不起。另外,你的第一条评论提到您遇到了映射错误 - 您需要定义这个从机构模型回到机构的新映射,它不会自动知道如何反转这个映射。
    【解决方案2】:

    是否可能需要将参数传递给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) 
    

    然后你必须将InstitutionModel 类型的对象和int 作为参数传递给它,否则你会得到一个异常。意思是,你需要

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

    【讨论】:

    • viewschool 详细信息有效,因为我正在返回我已将其映射到的模型
    • 我现在有这个:` [HttpPost] public ActionResult EditSchoolDetails(int?id, InstitutionModel authorityModel) { if (ModelState.IsValid) { var authority = _educationRepository.GetInstititionById(institutionModel.Id); // 从视图模型映射回领域模型 var model = Mapper.Map(institution);保存更改(); return RedirectToAction("ViewSchoolDetails"); } 返回视图(机构模型); }
    • 并且错误是:“/”应用程序中的服务器错误。 -------------------------------------------------- ------------------------------ 参数字典包含不可为空类型'System.'的参数'id'的空条目。 'NCARB.Web.Areas.Admin.Controllers.ToolsController' 中方法 'System.Web.Mvc.ActionResult ViewSchoolDetails(Int32)' 的 Int32'。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数
    • 我添加了这个:public ActionResult ViewSchoolDetails(int?id) { var authority = _educationRepository.GetInstititionById(id); var model = Mapper.Map(机构);返回视图(模型);
    • 这个改动根本没有运行
    【解决方案3】:

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

    InstitutionModel 有吗?

    【讨论】:

    • institutionModel.cs/ 类有:`public class InstitutionModel { public InstitutionModel() { NAABAccreditations = new List(); } 公共 int ID { 获取;放; } 公共字符串名称 { 获取;放; } public bool IsNAAB { get { return NAABAccreditations.Any(); } } 公共字符串网站 { 获取;放; } 公共地址模型地址 { 获取;放; } 公共 IEnumerable NAABAAccreditations { 获取;放; } } }
    • 那么 AddressModel 呢?或认证模型。你所有的视图模型都有一个吗?
    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多