0

Suppose I have a model named 'company' holding some properties like

  • Name
  • Address
  • fax


And My View (of IEnumarable type (MVC# RAZOR)) has two forms one(form submitting data to different actions) to display list all the available company(existing). And another(form) i used to create new client with Name TextBox
Like

@Html.TextBox("name","")


Here i want to add validation to that particular field.
Suggest me the possible easiest way??
What i tried is http://www.codeproject.com/Articles/39016/Form-validation-with-ASP-NET-MVC(But it doesn't works)

2
  • 1
    It should work. Could you be more elaborate on your error. What error you get ? Commented Dec 28, 2011 at 8:14
  • 1
    refreshed page with no error message Commented Dec 28, 2011 at 9:06

1 Answer 1

1

my view has two forms

Therefore you need two view models because I suppose that the validation rules are different for those 2 forms. So you will have a SearchCompanyViewModel where the Name field will not be required and a NewCompanyViewModel where the Name field will be required.

Sign up to request clarification or add additional context in comments.

4 Comments

Want to apply validation to 2nd form only
@Kannas, great, then you simply decorate your second view model properties with the necessary validation attributes.
I did it. I did all necessary stuff (suggested at mentioned link/ and few straight steps we generally follow) to apply validation. After doing all this, don't know what went wrong and my view returned back with no validation message
AAh at first Thanks Darin for your help.And secondly: i got success (i was returning RedirectToAction instead of View() ). Sometimes silly stuffs results in mess...newayz thankx

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.