2017-01-19 91 views
0

我目前正在使用C#和Asp.Net MVC开发的应用程序。我有一个分为两部分的表格。如果点击,每个按钮都有3个提交按钮,当然会执行客户端验证,并在需要一个或多个输入时进行投诉。客户端验证的部分表格

的如下点击:

  1. 提交 - 验证不应该是这样的形式
  2. 保存上半年 - 没有验证需要
  3. 暂时补充 - 验证对下半部分表格

我的视图模型类看起来像

public class ViewModel 
{ 
    public User User { get; set; } //used for first half of the form 
    public Department Department { get; set; } //used for second half of the form 
} 

和我的POCO类看起来像

public class User 
{ 
    public int Id { get; set; } 

    [Required] 
    public string Username { get; set; } 
    public DateTime Dob { get; set; } 

    //more required properties 
} 

public class Department 
{ 
    public int Id { get; set; } 

    [Required] 
    public string DepartmentName { get; set; } 

    //more required properties 
} 

保存点击,如果我有类cancel然后,似乎工作并没有被验证完成。不过,我似乎可以弄清楚如何为其他两个按钮做到这一点。有没有办法,或者我会完全脱离这里的铁路?

+0

你必须手动处理它。这种情况没有任何自动验证。 –

+0

@KD是否可以,如果你可以提供一个小例子。 – Code

+0

不清楚你想实现什么。你只想根据第一部分的某些条件来验证第二部分?你想保存第一部分之前的第二部分 –

回答

1

当你决定在你的评论中使用选项2时,这里是它的解决方案。

只要你有3个不同的按钮...

<button type="button" id="save" class="submit">Save</button> 
<button type="button" id="submit" class="submit">Submit</button> 
<button type="button" id="tempAdd" class="submit">Temporarily Add</button> 

你需要增加一个功能,并与所有上面的扣子绑定。

$(function(){ 
$("submit").click(function(){ 
    var ignoreValidationElements = []; 
    if$(this).attr("id") == "save") 
    { 
    // add element ids which you want to exclude from validation 
    ignoreValidationElements.push("FirstName"); 
    ignoreValidationElements.push("LastName"); 
    } 
    else if$(this).attr("id") == "submit") 
    { 
    ignoreValidationElements.push("FirstName"); 
    } 
    else if$(this).attr("id") == "tempAdd") 
    { 
    ignoreValidationElements.push("LastName"); 
    } 

    // code to remove validation attributes 
    for(i = 0; i < ignoreValidationElements.length;i++) 
    { 
     var element = $("#" + ignoreValidationElements[i]); 
     // remove all validation related attributes from it 
     var attributes = element[0].attributes; 
     for(var j = 0; j <attributes.length; j++) 
     { 
      if(attributes[j].name.indexOf("data-") >=0) 
      { 
       var attributItem = attributes[j]; 
       var attributeName = attributeItem.name; 
       element.removeAttr(attributeName); 

      } 
     } 

//submit the form 
    } 
    }); 

}); 
+0

的例子吗?你可以根据你的需要来增强这个逻辑,但是这个解决方案会给你一个关于在我的评论 –

+0

中提到的选项2的完整想法。谢谢你,这给了我一个好主意。但是,我收到一个错误,说'属性[j] .indexOf不是一个函数。此外,应该将表单提交给外部for'for循环 – Code

+0

我已修复该错误。谢谢你。很快,正如我所提到的,不应该将表单提交出循环? – Code