Uppercase attribute that converts the input to uppercase

前端 未结 4 1967
长发绾君心
长发绾君心 2020-12-18 01:36

I am working in MVC4 and want to define a model using an Uppercase attribute. The idea would be that the presence of the Uppercase attribute would cause the mod

4条回答
  •  天命终不由人
    2020-12-18 02:12

    NOTE: I'm adding on to this post because until I discovered the approach I now use, I read this and tried all above unsuccessfully.

    I generally use a two part process when dealing with forcing text data to be formatted as uppercase. 1. at the view and 2. at the controller

    1. At the view layer so that the user knows data is going to be used in the uppercase form. This can be down through htmlAttributes used in the EditorFor HTML helper.

      @HTML.EditorFor(model => model.Access_Code, new { htmlAttributes = new  Style= "text-transform:uppercase"}})
      

    Now this only forces the data seen and entered by the user to uppercase and not the data sent to the server. To do that requires some code in the associated method in the controller.

    1. I add the ToUpper() method to the target attribute of the object being passed back to the contoller. Here is hypothetical example showing this.

      public ActionResult verify(int? id)
            {
              var userData = db.user.Where (i=> i.userID == id).Single();
      
              userData.Access_Code = userData.Access_Code.ToUpper();
      
         ...
       }
      

提交回复
热议问题