简体   繁体   English

当控制器未明确传递给强类型视图时,视图模型为空

[英]View Model Null when not explicitly passed by controller to strongly typed view

my view is defined 我的观点是定义的

    @model BloombergGUI.Models.SecurityViewAltModel

    <div class="col-md-10">
        @Html.TextArea("TestArea",Model.FieldsList)
        @Html.TextAreaFor(m => m.FieldsList, new {@class = "form-control"})
    </div>

if the controller for this strongly typed view is defined as 如果此强类型视图的控制器定义为

    public ActionResult Index()
    {
        return View();  //The first Html.TextArea says Model.FieldList is null
    }

if it's defined as the following, then both statements in the view work. 如果它被定义为以下,则视图中的两个语句都有效。

    public ActionResult Index()
    {
        return View(new SecurityViewAltModel());
    }

Why when the view is strongly typed is Model.Property indicating Model is null but when I explicitly pass a new model() then Model.Property works fine. 为什么当强类型视图时,Model.Property指示Model为null但是当我显式传递一个新模型()时,Model.Property工作正常。 I thought Model was just another way of accessing the strongly typed model for the view and m=> m.property was a lambda expression for the TextBoxFor extension method used on strongly typed views. 我认为Model只是访问视图的强类型模型的另一种方式,m => m.property是用于强类型视图的TextBoxFor扩展方法的lambda表达式。

The

@model BloombergGUI.Models.SecurityViewAltModel

you define in the View is actually a strongly typed data passing mechanism from the controller. 您在View中定义实际上是来自控制器的强类型数据传递机制。

when you do 当你这样做

 return View(); 

you should be getting a NULL Model. 你应该得到一个NULL模型。 This is because no data is being passed to the View. 这是因为没有数据传递给View。 Model is expected to be null. 模型预计为空。

when you do 当你这样做

return View(new SecurityViewAltModel());

a non-null Model object is sent with all fields null. 发送非null Model对象,所有字段为null。 MVC will render empty controls for these null data fields. MVC将为这些空数据字段呈现空控件。

Note that in the second case, you might not get the Null reference exception because you're not dealing with a straight object.field reference, but an Expression . 请注意 ,在第二种情况下,您可能无法获得Null引用异常,因为您没有处理直接的object.field引用,而是处理Expression

m => m.FieldsList vs. Model.FieldsList m => m.FieldsListModel.FieldsList

Details: 细节:

  1. When the expression is being evaluated, there can be checks which will prevent the null reference. 在计算表达式时,可以进行检查以防止空引用。
  2. Deep inside the MVC DLLs, when the Expression is being processed, it has logic as follows: 在MVC DLL内部,当处理Expression时,它具有如下逻辑:

  3. Evaluate the Value of the Expression with Parameter as viewData.Model object. 使用Parameter评估表达式的值作为viewData.Model对象。

The call is as follows: 电话如下:

CachedExpressionCompiler.Process<TParameter, TValue>(expression)(model);

And at that time, it goes into the FingerprintingExpressionVisitor and it can handle the null Model and returns null response for the Extension helper to not render any data. 在那时,它进入FingerprintingExpressionVisitor并且它可以处理null Model并返回null响应以使Extension Helper不呈现任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM