删除空格或修剪在文本框中输入的字符串 [英] Remove whitespaces or trim the string entered in my textbox

查看:77
本文介绍了删除空格或修剪在文本框中输入的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修剪字符串或删除在mvc中名为VAREmail的textboxfor字段中输入的字符串的空格。使用的HTML代码是



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

<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; }





任何人都可以帮我解决这个错误。



我尝试了什么:



尝试在视图和控制器中使用trim()



can anyone please help me to fix this bug.

What I have tried:

tried using trim() in both view and controller

推荐答案

在我的控制器POST中你必须添加以下行:

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);
}


这篇关于删除空格或修剪在文本框中输入的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆