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 ());
/...
}
/...
}