Anjali Khan

Anjali Khan

  • NA
  • 867
  • 213.3k

why textbox is apper when i submit the data for the second.

Aug 30 2016 12:16 AM

 

Hi Frnds

i created Reg Form With Validation..this is working fine also..

in this form i added one textbox at run time...if i will get a error on a form that time it is showing..

and after filling the data if i will submit this form so text box is showing then gone..

i want to know that y it is showing then gone..

 

Model///

 public class Employee
    {
       //[Required(ErrorMessage = "Name is Required")]
      [Required]
        [Display(Name = "Name")]
        //[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Name should be accept Char only")]
        [RegularExpression(@"^.{6,10}$", ErrorMessage = "Name should be of minimum 6 and maximum 10 characters")]


        public string Name { get; set; }

        [Required(ErrorMessage = "Email is Required")]
        [Display(Name = "Email")]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
          ErrorMessage = "Email is not valid")]
       
        public string Email { get; set; }
       
        [Required(ErrorMessage = "Password is Required")]
        [Display(Name = "Password")]
        [RegularExpression(@"^(?=.*\d)(?=.*[a-zA-Z]).{4,8}$", ErrorMessage = "Password Should be contain atleast 1 Upper, 1 Lower and 1 number")]
       
        //Password Should be contain atleast 1 Upper, 1 Lower and 1 Special Character
        public string Password { get; set; }     
    }


Controller///


 public class EmployeeRegFormController : Controller
    {
        // GET: EmployeeRegForm
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(Employee model)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Name = model.Name;
                ViewBag.Email = model.Email;
                ViewBag.Password = model.Password;
            }
           if(model.Name == null)
            {
                ModelState.AddModelError("", "Details  is incorrect!");
            }
            return View(model);     
       }     
    }


Index/view/

@if (ViewData.ModelState.IsValid)
{
    if (@ViewBag.Name != null)
    {
        <b>
            <span style="font-family: Verdana; font-size: 10pt;">
                Name : @ViewBag.Name<br />
                Email : @ViewBag.Email<br />
            </span>
        </b>
    }
}
<br />
<br />

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <div>
        <fieldset>
            <legend>Employee</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">

                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name, "", new { @Style = "color:red" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email, "", new { @Style = "color:red" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Password)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Password, new { maxlength = 20, value = ViewBag.Selpwd })
                @Html.ValidationMessageFor(model => model.Password, "", new { @Style = "color:red" })
            </div>
            <p>
                <input id="btnAdd" type="SUBMIT" value="Register" onclick="AddTextBox()" />
            </p>
            <div id="TextBoxContainer">
                <!--Textboxes will be added here -->
            </div>

        </fieldset>  
    </div>
}
   <script type="text/javascript">

               function GetDynamicTextBox(value){
               return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />'            
           }
              var count=0;
              function AddTextBox()
              {
                  if (count <= 1) {
                      var div = document.createElement('DIV');
                      div.innerHTML = GetDynamicTextBox("");
                      document.getElementById("TextBoxContainer").appendChild(div);
                  }
              }
</script>


Answers (9)