简体   繁体   中英

“A namespace cannot directly contain members such as fields or methods” File: controller with context

<#@ template language="C#" HostSpecific="True" #>
<#
var Model = (MvcTextTemplateHost)Host;
var routePrefix = String.Empty;
if (!String.IsNullOrWhiteSpace(Model.AreaName)) {
routePrefix = "/" + Model.AreaName;
}
routePrefix += "/" + Model.ControllerRootName + "/";
#>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.Objects" #>
<#@ import namespace="System.Linq" #>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
<# if(MvcTextTemplateHost.NamespaceNeeded(Model.Namespace,         Model.ModelType.Namespace)) { #>
using <#= Model.ModelType.Namespace #>;
<# } #>
<# if(Model.ContextType.Namespace != Model.ModelType.Namespace &&   MvcTextTemplateHost.NamespaceNeeded(Model.Namespace,  Model.ContextType.Namespace)) { #>
using <#= Model.ContextType.Namespace #>;
<# } #>
namespace <#= Model.Namespace #>
{
<#
var modelName = Model.ModelType.Name;
var entitySetName = Model.EntitySetName;
var modelVariable = modelName.ToLower();
var entitySetVariable = entitySetName.ToLower();
var primaryKey = Model.PrimaryKeys[0];
var lambdaVar = modelVariable[0];
var isObjectContext =   typeof(ObjectContext).IsAssignableFrom(Model.ContextType);
#>
public class <#= Model.ControllerName #> : BootstrapBaseController
{
    private <#= Model.ContextType.Name #> db = new <#= Model.ContextType.Name #>();

    //
    // GET: <#= routePrefix #>

    public ActionResult Index()
    {
<#  var includeExpressions = "";
if(isObjectContext) {
    includeExpressions = String.Join("",  Model.RelatedProperties.Values.Select(property => String.Format(".Include(\" {0}\")", property.PropertyName)));
}
else {
    includeExpressions = String.Join("", Model.RelatedProperties.Values.Select(property => String.Format(".Include({0} =>  {0}.{1})", lambdaVar, property.PropertyName)));
}
#>
# if(!String.IsNullOrEmpty(includeExpressions)) { #>
        var <#= entitySetVariable #> = db.<#= entitySetName #><#=    includeExpressions #>;
        return View(<#= entitySetVariable #>.ToList());
<# } else { #>
        return View(db.<#= entitySetName #><#= includeExpressions   #>.ToList());
<# } #>
    }

    //
    // GET: <#= routePrefix #>Details/5

    public ActionResult Details(<#= primaryKey.ShortTypeName #> id)
    {
<# if(isObjectContext) { #>
        <#= modelName #> <#= modelVariable #> = db.<#= entitySetName   #>.Single(<#= lambdaVar #> => <#= lambdaVar #>.<#= primaryKey.Name #> == id);
<# } else { #>
        <#= modelName #> <#= modelVariable #> = db.<#= entitySetName  #>.Find(id);
<# } #>
        if (<#= modelVariable #> == null)
        {
            return HttpNotFound();
        }
        return View(<#= modelVariable #>);
    }

    //
    // GET: <#= routePrefix #>Create

    public ActionResult Create()
    {
<# foreach (var property in Model.RelatedProperties.Values) { #>
        ViewBag.<#= property.ForeignKeyPropertyName #> = new SelectList(db.  <#= property.EntitySetName #>, "<#= property.PrimaryKey #>", "<#=  property.DisplayPropertyName #>");
<# } #>
        return View(new <#= modelName #>());
    }

    //
    // POST: <#= routePrefix #>Create

    [HttpPost]
    public ActionResult Create(<#= modelName #> <#= modelVariable #>)
    {
        if (ModelState.IsValid)
        {
<# if(primaryKey.Type == typeof(Guid)) { #>
            <#= modelVariable #>.<#= primaryKey.Name #> = Guid.NewGuid();
<# } #>
<# if(isObjectContext) { #>
            db.<#= entitySetName #>.AddObject(<#= modelVariable #>);
<# } else { #>
            db.<#= entitySetName #>.Add(<#= modelVariable #>);
<# } #>
            db.SaveChanges();
            return RedirectToAction("Index");
        }

<# foreach (var property in Model.RelatedProperties.Values) { #>
        ViewBag.<#= property.ForeignKeyPropertyName #> = new SelectList(db.  <#= property.EntitySetName #>, "<#= property.PrimaryKey #>", "<#=  property.DisplayPropertyName #>", <#= modelVariable #>.<#=  property.ForeignKeyPropertyName #>);
<# } #>
        return View(<#= modelVariable #>);
    }

    //
    // GET: <#= routePrefix #>Edit/5

    public ActionResult Edit(<#= primaryKey.ShortTypeName #> id = <#=  primaryKey.DefaultValue #>)
    {
<# if(isObjectContext) { #>
        <#= modelName #> <#= modelVariable #> = db.<#= entitySetName   #>.Single(<#= lambdaVar #> => <#= lambdaVar #>.<#= primaryKey.Name #> == id);
<# } else { #>
        <#= modelName #> <#= modelVariable #> = db.<#= entitySetName  #>.Find(id);
<# } #>
        if (<#= modelVariable #> == null)
        {
            return HttpNotFound();
        }
<# foreach (var property in Model.RelatedProperties.Values) { #>
        ViewBag.<#= property.ForeignKeyPropertyName #> = new SelectList(db.   <#= property.EntitySetName #>, "<#= property.PrimaryKey #>", "<#=  property.DisplayPropertyName #>", <#= modelVariable #>.<#=  property.ForeignKeyPropertyName #>);
<# } #>
        return View("Create", <#= modelVariable #>);
    }

    //
    // POST: <#= routePrefix #>Edit/5

    [HttpPost]
    public ActionResult Edit(<#= modelName #> <#= modelVariable #>)
    {
        if (ModelState.IsValid)
        {
<# if(isObjectContext) { #>
            db.<#= entitySetName #>.Attach(<#= modelVariable #>);
            db.ObjectStateManager.ChangeObjectState(<#= modelVariable #>,  EntityState.Modified);
<# } else { #>
            db.Entry(<#= modelVariable #>).State = EntityState.Modified;
<# } #>
            db.SaveChanges();
            return RedirectToAction("Index");
        }
<# foreach (var property in Model.RelatedProperties.Values) { #>
        ViewBag.<#= property.ForeignKeyPropertyName #> = new SelectList(db.   <#= property.EntitySetName #>, "<#= property.PrimaryKey #>", "<#= property.DisplayPropertyName #>", <#= modelVariable #>.<#= property.ForeignKeyPropertyName #>);
<# } #>
        return View("Create", <#= modelVariable #>);
    }

    //
    // GET: <#= routePrefix #>Delete/5

    public ActionResult Delete(<#= primaryKey.ShortTypeName #> id)
    {
<# if(isObjectContext) { #>
        <#= modelName #> <#= modelVariable #> = db.<#= entitySetName   #>.Single(<#= lambdaVar #> => <#= lambdaVar #>.<#= primaryKey.Name #> == id);
        db.<#= entitySetName #>.DeleteObject(<#= modelVariable #>);
<# } else { #>
        <#= modelName #> <#= modelVariable #> = db.<#= entitySetName #>.Find(id);
        db.<#= entitySetName #>.Remove(<#= modelVariable #>);
    <# } #>
            db.SaveChanges();
            return RedirectToAction("Index");
        }
            protected override void Dispose(bool disposing)
            {
               db.Dispose();
               base.Dispose(disposing);
            }
        }
    }

This code was auto generated under my code template file in my Task1 ASP.NET MVC Application, but whenever I execute my project, I getting the above error on line 1 and col 1 in this content, Please Provide me solutions to resolve this error.

You can reproduce the problem with just the snippet below.

<#@ template language="C#" HostSpecific="True" #>
<#
var Model = (MvcTextTemplateHost)Host;
#>

The MvcTextTemplateHost type cannot be resolved an causes the error you see. There are quite a few Google results with potential solutions, most of which involve ensuring your project has a valid reference to Microsoft.VisualStudio.Web.Mvc.3.01.dll of one version or another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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