【问题标题】:ASP.Net 4.5 error on compilingASP.Net 4.5 编译错误
【发布时间】:2014-10-30 17:20:42
【问题描述】:

以下是我的代码,编译时出现 3 个错误,如图所示

任何帮助都将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;

namespace Build01
{
    public static class ModelBindingExtensions
    {

        public static ModelBindingExecutionContext GetModelBindingExecutionContext(this Page page)
        {
            return new ModelBindingExecutionContext
            {
                HttpContext = new HttpContextWrapper(HttpContext.Current),
                ModelState = page.ModelState
            };
        }
    }
}

错误 1 ​​'System.Web.ModelBinding.ModelBindingExecutionContext' 确实 不包含接受 0 个参数的构造函数错误 2 属性或 无法将索引器“HttpContext”分配给 - 它是只读的
错误 3 属性或索引器“ModelState”无法分配给 -- 它 是只读的

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    ModelBindingExecutionContext 没有采用 0 个参数的构造函数。 But it does have one that takes both a HttpContext and a ModelStateDictionary,所以你需要将它们传递给构造函数:

    return new ModelBindingExecutionContext(
        new HttpContextWrapper(HttpContext.Current), page.ModelState);
    

    【讨论】:

      【解决方案2】:

      看起来您使用类构造函数的语法不正确。而不是使用{ },而是使用( )

          public static ModelBindingExecutionContext GetModelBindingExecutionContext(this Page page)
          {
              return new ModelBindingExecutionContext
              ( // note ( not {
                  new HttpContextWrapper(HttpContext.Current),
                  page.ModelState
              );
          }
      

      { } 的语法可用于设置类属性,但前提是它们具有公共设置器。在这种情况下HttpContextModelState 需要通过对象构造函数设置,并且没有公共设置器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-05
        • 2014-12-03
        • 2016-01-31
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多