2009-01-03 46 views
1

我有一个用于编辑演出的窗体。.net mvc和用表格编辑数据

初始控制器操作称为“编辑”。

形式职位,以所谓的“更新”

所以,一旦形式已经发布,我用它使用bindingContext.ModelState.AddModelError来验证消息添加到的ModelState一个定制ModelBinder的第二控制器动作

更新copntroller动作看起来这样:

​​

如果ModelBinder的有错误的异常将被更新模型被抛出。

这意味着调用RedirectToAction(“Edit”),以便调用原始的“Edit”控制器操作。

这意味着我不会看到我的验证消息,用户添加到表单中的任何数据都将重置为原始值!

我应该如何处理这个问题?

我已经包含下面的“编辑”行动:

[AcceptVerbs("GET")] 
    public ActionResult Edit(Guid id) 
    { 
     Gig gig = GigManager.GetByID(id); 

     SelectList days = CreateDays(1, 31, 1, gig.StartDate.Day); 
     ViewData["day"] = days; 

     SelectList months = CreateMonths(1, 12, 1, gig.StartDate.Month); 
     ViewData["month"] = months; 

     SelectList years = CreateYears(DateTime.Now.Year, DateTime.Now.Year + 10, 1, gig.StartDate.Year); 
     ViewData["year"] = years; 

     string bandNames =""; 
     string bandIds = ""; 
     foreach(Act act in gig.Acts) 
     { 
      bandNames += act.Name.Trim() + ", "; 
      if (act.Artist != null) 
      { 
       bandIds += act.Artist.ID + ";"; 
      } 
     } 

     ViewData["Bands"] = bandNames; 
     ViewData["BandIds"] = bandIds; 

     return View(gig); 

    } 

不过,我不明白个

回答

1

验证消息,也许这将帮助。我刚刚提交了一个控制器,它可以执行列表/编辑管理员。它在可能方便的类上使用绑定。查看文件的最后部分以查看处理Get和Post Verbs的可能方式。请注意,UpdateModelStateWithViolations只是将错误添加到ModelState的帮手。

 Controller.ModelState.AddModelError(violation.PropertyName, 
      violation.ErrorMessage); 

其中显示有

<%= Html.ValidationSummary() %> 

http://www.codeplex.com/unifico/SourceControl/changeset/view/1629#44699

和查看:http://www.codeplex.com/unifico/SourceControl/changeset/view/1629#54418

[AcceptVerbs("GET")] 
    [Authorize(Roles = "Admin")] 
    public ActionResult EditRole(Guid? RoleID) 
    { 
     Role role = null; 
     RoleForm form = new RoleForm { }; 
     if (RoleID.HasValue) 
     { 
      role = accountService.GetRole(RoleID.Value); 
      if (role == null) 
       return RedirectToAction("Roles"); 

      form = new RoleForm 
      { 
       RoleID = role.ID, 
       RoleName = role.Name, 
       Level = role.Level 
      }; 
     } 
     else 
     { 
      form = new RoleForm(); 
     } 

     ViewData.Model = form; 

     return this.PluginView(); 
    } 


    [AcceptVerbs("POST")] 
    [Authorize(Roles = "Admin")] 
    public ActionResult EditRole(Guid? RoleID, [Bind(Include = "RoleID,RoleName,Level", Prefix = "")] RoleForm form) 
    { 
     Role role = null; 
     if (RoleID.HasValue) 
     { 
      role = accountService.GetRole(RoleID.Value); 
      if (role == null) 
       return RedirectToAction("Roles"); 
     } 

     ServiceResponse<Role> response = accountService.AttemptEdit(form); 

     if (response.Successful) 
     { 
      TempData["Message"] = "Update Successfull"; 
     } 
     else 
     { 
      this.UpdateModelStateWithViolations(response.RuleViolations); 
     } 

     //ViewData["AllRoles"] = accountService.GetRolePage(new PageRequest(0, 50, "Name", typeof(string), true)).Page.ToArray(); 


     ViewData.Model = form; 

     return this.PluginView(); 
    }