The method of removing space between trim and Str.trim in asp.net

Source: Internet
Author: User
Tags trim

Str.trim () is the use of. Net
Trim (str) is the use of VB. This usage cannot be used in C #, but the one above can be used for C #

The string parameter in the action, such as the name parameter in the Query method.
The string property of the complex type parameter in the action, such as the Name property of the person in the Create method.
The string property of the complex type that is explicitly bound in the action, such as the person's Name property in the third action.

Public class Personcontroller:controller
{
    public actionresult query (string Name)
    {
       //...
   }
   /...
    [HttpPost]
    public actionresult Create (person person)
  & nbsp {
       //...
   }
    [HttpPost]
    public actionresult Create (formcollection collection)
& nbsp;   {
        person is = new person ();
 updatemodel (person, collection);
       /...
   }
   /...
}

Public class person
{
    public int id {get; set;}
    public string name {get; set;}
}

Create a Modelbinder with trim functionality (string type only):

Public
class Stringtrimmodelbinder:defaultmodelbinder
{
public override Object Bindmodel (ControllerContext controllercontext, Modelbindingcontext BindingContext)
{
var value = Base.bindmodel (ControllerContext, BindingContext);
if (value is String) return (value as String). Trim ();
return value;
}
}

Specify this modelbinder for string type in Global.asax:

public class Mvcapplication:system.web.httpapplication
{
    protected void app Lication_start ()
    {
        modelbinders.binders.add ( typeof (String), New Stringtrimmodelbinder ());
       /...
   }
   /...
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.