我目前正在开发使用C#和Asp.Net MVC开发的应用程序。我有一个分为两部分的表格。如果点击,每个提交按钮有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 :(得分:1)
当您决定在评论中使用选项2时,这是解决方案。
如果您有3个不同的按钮......
$(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
}
});
});
你必须添加一个功能并将其与所有上述按钮绑定。
rpm