ASP.NET MVC EnableClientValidation

时间:2010-06-06 09:21:46

标签: asp.net-mvc data-annotations

这个<% Html.EnableClientValidation(); %>真的能让客户验证没有页面刷新的验证吗?

问题是它在返回formviewmodal时正在进行页面刷新,使用模态时工作正常吗?那么我将如何使用formviewmodal

使其工作

这是控制器返回FormViewModel的代码

public class OrganizationGroupFormViewModel
{      
    public OrganizationGroup OrganizationGroups { get; set; }
    public OrganizationGroupFormViewModel() { }

    public OrganizationGroupFormViewModel(OrganizationGroup OrganizationG)
    {
        OrganizationGroups = OrganizationG; 
    }
}

public class OrganizationGroupsController : Controller
{
    public ActionResult Create()
    {       
        OrganizationGroup OrgGroup = new OrganizationGroup
        {
            int_CreatedBy = Authorization.UserID,
            dtm_CreatedDate = DateTime.Now
        };
        return View(new OrganizationGroupFormViewModel(OrgGroup));     
    }

    [HttpPost]
    public ActionResult Create(OrganizationGroup OrgGroup)
    {
        try
        {
          if (ModelState.IsValid)
           {
             OrgGroup.int_CreatedBy = Authorization.UserID;
             OrgGroup.dtm_CreatedDate = DateTime.Now;

             OrganizationGroupRepository.Add(OrgGroup);
             OrganizationGroupRepository.Save();

             return View(new OrganizationGroupFormViewModel(OrgGroup));
          }
          else
              return View(new OrganizationGroupFormViewModel(OrgGroup));
        }
        catch
        {
            return View(new OrganizationGroupFormViewModel(OrgGroup));
        }
    }

1 个答案:

答案 0 :(得分:0)

是的,这个帮助方法将相应的javascript添加到页面中以启用客户端验证。您需要根据您使用的框架包含正确的js脚本。看看this blog post