MVC Foolproof验证 - 无法读取属性'值'未定义的

时间:2015-11-25 07:34:08

标签: asp.net-mvc-4 unobtrusive-validation foolproof-validation

我在我的应用程序中使用MVC Foolproof validation。 场景是我有一个名为CustomerType的下拉列表,其中包含以下值

 Id     Name
 1      Student
 2      Non-Employed
 3      Employed
 4      SelfEmployed

我的视图模型public string CompanyAddress{ get; set; }中还有一个属性。我的目标是使公司地址required if下拉列表具有值3 or 4

我试过以下

   [Required(ErrorMessage = "Please select status of the customer", AllowEmptyStrings = false)]
    public int? customerTypeID { get; set; }
    public SelectList customerTypeList { get; set; }

   [RequiredIf("IsCompanyAddressRequired", true, ErrorMessage = "please enter company address")]
    public string CompanyAddress { get; set; }


 public bool IsCompanyAddressRequired
    {
        get
        {
            if (customerTypeID == 3 || customerTypeID == 4)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

    }

以上代码在服务器端正常运行,但在客户端,我收到以下错误

 `Uncaught TypeError: Cannot read property 'value' of undefined`

验证的引用如下

 bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/plugins/jQueryVal/jquery.unobtrusive*",
                    "~/plugins/jQueryVal/jquery.validate*",
                    "~/plugins/foolproofValidation/MvcFoolproofJQueryValidation.min.js",
                    "~/plugins/foolproofValidation/mvcfoolproof.unobtrusive.min.js",
                    "~/plugins/foolproofValidation/MvcFoolproofValidation.min.js"
                    ));

1 个答案:

答案 0 :(得分:6)

您的验证属性无法在客户端上的客户端上运行,除非您要生成输入(例如)public class Coffee { //Instance Variables private double sugar; private int milk; private boolean isHot; // renamed heat to isHot private int size; // Constructor public Coffee (double id, int dairy, boolean temp ) { sugar = id; milk = dairy; heat = temp; } // (setter) public void setSugar(double id) { this.sugar = id; } public void setMilk(int dairy) { this.milk = dairy; } public void setSize(int size) { this.size = size; } public void setHeat(boolean isHot) { this.isHot = isHot; } //(getter) public double getSugar() { return this.sugar; } public int getMilk() { return this.milk; } public boolean checkIfHot() { return this.isHot; } public int getSize() { return this.size; } // Method to display data, (need to work on this) static void display() { System.out.println("You added " + getSugar() + " tablespoons of sugar to your coffee"); System.out.println("You have " + getMilk() + " in your coffee"); System.out.println("That's a " + getSize() + " ounce cup"); System.out.println("Is the cup hot? " + checkIfHot()); } // Default Constructor (need help with setting heat to a default) public Coffee() { sugar = 0.0; milk = 0; heat = false; size = 0; } } (或<input name="IsCompanyAddressRequired" value="false" />,具体取决于value="true"的初始值)和然后在下拉列表值更改时使用javascript动态更新value属性。

改为使用

customerTypeID

并删除您的[RequiredIf("customerTypeID", Operator.GreaterThan, 2, ErrorMessage = "..")] public string CompanyAddress { get; set; } 媒体资源。