0

in my very first asp.net mvc4 project,i'm trying to apply some validation tests on my four forms in the same page.Each form allows me to create a different type of project in my table(dataBase),so i'm using the same model generated by entity framework and for validation i used a partial class having the same name of my generated model and i've inserted data annotation such as required with error messages.My problem is when i submit one form leaving a required field empty,the appropriate validation error message is shown for all my forms and not only the one i submitted.Please can someone tell me how can i solve this problem?

1
  • in order to avoid such problem just take multiple forms on view.. Commented Aug 8, 2014 at 10:04

2 Answers 2

1

If you have a RequiredAttribute on (say) property Name and its null on postback, a validation error is added for that property. If you inspect the html generated for the form, you will see something like this (assuming your using @Html.ValidationMessageFor(m => m.Name)) which acts as a placeholder for any validation error message associated with Name

<span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>

Note the ..data-valmsg-for="Name"... Since your rendering 4 identical forms using the same model which generates 4 inputs for property Name, there will be 4 corresponding validation errors.

Your approach in rendering 4 identical forms does not make sense since you can only post one back. I suggest you consider using a view model that includes a property for ProjectType and render a DropDownList so the user can select the project type. Then only one form is required, and when submitting, get the selected ProjectType and use this to make whatever decisions you need to save the data.

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

5 Comments

i understand what you said about validation errors,but i don't think i'm able to use one form,because in my case one ProjectType has some fields that the other ProjectType doesn't have,so i found myself obliged to make four forms.i changed my approach to use 4 models each one represents a ProjectType(i used TPH inheritance) and i planned to create a partial class with data Annotation for each model(in a viewModel),but then my partial classes where not used,and when i tried it with the project partial class,displayName works but not the Required dataAnnotation,don't know what i'm doing wrong.
If each project type has different properties/attributes, then they definitely should be separate models, but more importantly separate actions and views. You should not be trying to combine these in one view with 4 forms (use separate views for CreateProjectType1, CreateProjectType2, ..)
yess,but i'm obliged to combine them in one view because our client asked for it.
Then create a view model which has 4 properties, one for each project type, public ProjectType1 Project1 { get; set; } etc, then render the inputs as @Html.TextBoxFor(m => m.Project1.Name), @Html.TextBoxFor(m => m.Project2.Name) etc so the the controls have distinct names and distinct associated validation controls.
i've already done that, but the required dataAnnotation is not working while display dataAnnotation is.I created my 4 ProjectType entities as objects inheriting from Project entity that has a table in my dataBase
0

Just structure your View in this way :-

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { @Id = "Form1" }))
{
  ..........
  ..........
  <button type="submit">Update this stuff</button>
}

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { @Id = "Form2" }))
{
  ..........
  ..........
  <button type="submit">Update this stuff</button>
}

In above example when you submit "Form1" model validations associated with "Form1" will fire and same case for "Form2".

6 Comments

my view is already structured that way,all my four forms have different ids.don't know where the problem is!!
@sassa92...strange this should not happen just check your beginform tags their opening and closing brackets and each beginform should have their separate submit button inside beginform form braces..
all my forms are considered as one form,because when i fill a field in one form the same field in the other forms is filled with the same value.i'm thinking maybe this happend because i used partial class,i'll try my previous version of the project without it and i'll let you know.
@Exception..actually my previous project works just fine without the partial class and validation stuff don't know what's wrong.maybe i should change my method of validation,but the only way i could find is the partial class method,do you have any idea ??
@sassa92...no i have never used partial class in mvc..we just make simple model classes for views and they work great..!!!
|

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.