1

i am new to mvc. i have just encountered a problem. I have created a prtial view which has a texbox in it, which is bind to a property in a model. on that property [Required] validation has applied. the problem is that when i render that partial view multiple times on a view and clik the submit buttion after filling data only in one text box, validation applies on all partial views at the same time. What i want is that [Required] Validation should apply on all partial views/TextBoxes seperately.

My partial View is

@Html.TextboxFor(m=>m.Name)

Model is

[Required]
public string Name(get; set;}

My view has multiple partial views

 @Html.Partial(_MyPartialView)
 @Html.Partial(_MyPartialView)
<input Type="submit" value="click"/>

when i fill data on first partial then validation automatically apply on second partial view too at the same time.

Hope you guys will understand the preoblme. I will be really thankfull if any one can help as i am stuck for several days for this issue. Many thanks in advance.

1 Answers1

0

It's hard to understand what you want to do , because if you have a modal that contains one property named NAME so how I understand you have a form and inside this form you a rendering same partial view with only property that you have , so what you are expected to get on POST method ? I thing you should change your modal to have a list of string like this:

public class Model
{
  public IList<Name> Names{get;set;}
  public Model()
  {
        Names=new List<Name>();
  }
}
public class Name
{
    [Required]
    public string FirstName{get;set;
}

and now in view you can make something like this : View:

@{
   Layout=null;
}
  @model Model
   @using (Ajax.BeginForm("Action", "Controller", null,
                        new AjaxOptions { HttpMethod = "POST"} ))
 {
    @Html.TextBoxFor(c=>c.Names[0].FirstName)
    @Html.TextBoxFor(c=>c.Names[1].FirstName)
    //and so on 

   <input type="submit" value="submitForm"/>
}

Do not forget to include all necessary scripts for mvc client validation . Hope this will help you .

Nic
  • 1,088
  • 3
  • 19
  • 43
  • Thank u so much MDDDC for your reply. i know its hard to understand. basically i have posted a very simplified version of my problem.. i think i should rephrase my problem again. – MVC Learner Feb 24 '14 at 18:54
  • try to understand this issue like that i have a partial view which has to be used on other views multiple times.. on that partial view i have a textbox which take special type of input.. i have made partial view for that bc i have to reuse that special type of textbox again and again in my application.. in partial view there is a single text box which is bind to a single property of model.. but when i placed or reuse that partial view multiple times on any view that validation problem occurs which i had mentioned in my question..i hope now u will get a better picture of this issue. – MVC Learner Feb 24 '14 at 18:56
  • take a look at mvc editor templates ,I really don't like them but this is what you are talking about...I don't now how to say but validation is is closely to model of form,without form this will not work.... – Nic Feb 24 '14 at 20:53