zoukankan      html  css  js  c++  java
  • Understanding Optional and Compulsory Parameters

    If the MVC Framework cannot find a value for a reference type parameter (such as a string or object), the action
    method will still be called, but using a null value for that parameter. If a value cannot be found for a value type parameter (such
    as int or double), then an exception will be thrown, and the action method will not be called. Here is another way to think
    about it:

    Value-type parameters are compulsory. To make them optional, either specify a default value (see the next section) or
    change the parameter type to a nullable type (such as int? or DateTime?), so the MVC Framework can pass
    null if no value is available.

    值类型可以指定默认值,或指定值类型可以为null。

    public ActionResoult Index(int ? Age,string Name)

    or

    public ActionResoult Index(int  Age = 18 , string Name = "jack")

    应验证数据是否为null。

    如果值无法转换为指定类型,ModelState将置为false。

    Reference-type parameters are optional. To make them compulsory (to ensure that a non-null value is passed), add
    some code to the top of the action method to reject null values. For example, if the value equals null, throw an
    ArgumentNullException.

    引用类型应验证数据是否为null。

  • 相关阅读:
    解决函数内this指向
    .Math 数值对象
    时间函数
    数学中的弧度和角度
    闭包
    在拖拽元素的时候,如果元素的内部加了文字或者图片,拖拽效果会失灵?
    正则
    JS高级-事件对象
    JS高级-事件捕捉
    JS高级-面向对象
  • 原文地址:https://www.cnblogs.com/dotnetmvc/p/3822200.html
Copyright © 2011-2022 走看看