0

I need following attributes:

1.For example: i have 2 field. first is checkbox, second is textbox. If first control checked. second field must be Required attribute. first control unchecked. second control not required.

[Required]
public boolean showHeader{get;set;}

[IFRequired("showHeader",true)]
public string HeaderText{get;set;}

2.For example: i have 2 field. new password, confirmation password. Attribute must check this 2 field are equal.

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

[Expression("newPassword",ExpressionAttributeEnum.Equils)]
public string confirmPassword{get;set;}

How to create above attributes?

2 Answers 2

0

This is not possible using attributes.

however you can do it quite easy in your action.

public ActionResult Create(FormCollection collection) {
    ///do your checks here which you cant do using attributes
    ModelState.AddModelError("fieldname", "yourErrorMessage");

    if (ModelState.IsValid) {
        ////.........
    }
    return View();
}
Sign up to request clarification or add additional context in comments.

Comments

0

For point 2 if you're using MVC3 use the Compare attribute

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

[Compare(] public string confirmPassword{get;set;}

Have a look at this SO question and answer for the first part

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.