Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to trim the string or remove whitespaces for the string entered in my textboxfor field called "VAREmail" in mvc. The html code used is

HTML
<div class="detailsRow">
           <div class="detailsLabel">
               @Html.LabelFor(m => m.VarEmail, Strings.VarEmail)
           </div>
               @Html.TextBoxFor(m => m.VarEmail, new { style = "width:50%" })
               @Html.ValidationMessageFor(m => m.VarEmail)

       </div>


The controller code triggered when same button is clicked is

[HttpPost]
       public ActionResult Edit(Supplier supplier)
       {

           if (!ModelState.IsValid)
           {
               supplier = GetSupplier(supplier.SsoId);

               return View("Edit", supplier);
           }

           ReviewerDto dto = googleReviewService.GetReviewerBySsoId(supplier.SsoId);

           dto.GoogleReviewEmail = supplier.EmailAddress;
           dto.MorpheusId = supplier.MorpheusId;
           dto.VarEmail = supplier.VarEmail;
           dto.SesameEmail = supplier.SesameEmail;
           dto.SesameFirstName = supplier.SesameFirstName;
           dto.SesamePassword = supplier.SesamePassword;
           dto.VettedReviewer = supplier.VettedReviewer;

           googleReviewService.UpdateReviewer(dto);

           supplier
               .ReviewerRates
               .ToList()
               .ForEach(rr => googleReviewService.SetReviewerRateActiveStatus(supplier.SsoId, rr.RateId, rr.Active));

           return RedirectToAction("List");
       }


the model used with some regular expression fro validation is

[StringLength(50)]
      [RegularExpression(@"^(?i)[A-Z0-9._%+-]+@[A-Z0-9]+.com", ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "ReviewerEditModel_InvalidEmailEntered")]
      [Display(ResourceType = typeof(Strings), Name = "VarEmail")]
      public string VarEmail { get; set; }


can anyone please help me to fix this bug.

What I have tried:

tried using trim() in both view and controller
Posted
Updated 14-Mar-17 7:00am
v4
Comments
Karthik_Mahalingam 20-May-16 4:48am    
show the line where you are using trim()
user 3008 20-May-16 4:52am    
dto.VarEmail = supplier.VarEmail.trim();
and also

@Html.TextBoxFor(m => m.VarEmail, Model.VarEmail.Trim(), new { style = "width:50%" })

Karthik_Mahalingam 20-May-16 4:56am    
did u check the value by placing breakpoint at that line ?
user 3008 20-May-16 4:57am    
yes tried that but it is not trimming
Karthik_Mahalingam 20-May-16 6:26am    
Always use  Reply  button, to post Comments/query to the user, else the User wont get notified.

1 solution

In my opinio in the controller POST you must add the line:
supplier.VarEmail=supplier.VarEmail.Trim();

just before
if (!ModelState.IsValid)
{
    supplier = GetSupplier(supplier.SsoId);

    return View("Edit", supplier);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900