Question

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.

Was it helpful?

Solution

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 .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top