PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] How to apply validation on multiple partail views which are bind to same property of model in mvc?

 May 18, 2022     model-view-controller, partial, partialviews, validation, views     No comments   

Issue

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.


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 .



Answered By - Nic
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing