视图模型空时不受控制器明确地传递给强类型的视图 [英] View Model Null when not explicitly passed by controller to strongly typed view

查看:166
本文介绍了视图模型空时不受控制器明确地传递给强类型的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的看法是定义

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

为什么当视图是强类型的Model.Property表明型号为空,但是当我明确地传递一个新的模式(),然后Model.Property工作正常。我以为模型访问的强类型模型视图和M => m.property是一个lambda前pression所使用的强类型的意见TextBoxFor扩展方法的另一种方式。

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. 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 BloombergGUI.Models.SecurityViewAltModel

您在视图定义实际上是一个强类型的数据传递从控制器机制。

you define in the View is actually a strongly typed data passing mechanism from the controller.

当你做

 return View(); 

你应该得到一个空模型。这是因为没有数据传递到视图。模式有望为null。

you should be getting a NULL Model. This is because no data is being passed to the View. Model is expected to be null.

当你做

return View(new SecurityViewAltModel());

一个非空的Model对象与空各个领域的发送。 MVC将呈现空控制这些空数据字段。

a non-null Model object is sent with all fields null. MVC will render empty controls for these null data fields.

注意,在第二种情况下,你可能无法得到的空引用异常,因为你不处理直object.field参考,但一个前pression

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.

M =&GT; m.FieldsList Model.FieldsList

详细内容:


  1. 当前pression正在接受评估,可能有检查,这将prevent空引用。

  2. 内心深处的MVC的DLL,当被处理的前pression,它有逻辑如下:

  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:

评估前pression 与参数的值作为 viewData.Model 对象。

Evaluate the Value of the Expression with Parameter as viewData.Model object.

通话如下:

CachedEx pressionCompiler.Process&LT; TParameter,TValue&GT;(例如pression)(模型);

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

而在那个时候,它进入 FingerprintingEx pressionVisitor 并可以处理空模型并返回扩展帮手不提供任何数据空响应。

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.

这篇关于视图模型空时不受控制器明确地传递给强类型的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆