【问题标题】:MVC4 add overload to a methodMVC4 为方法添加重载
【发布时间】:2013-08-12 15:07:05
【问题描述】:

你们中的许多人都知道 MVC4 有一些很棒的新功能,我正在努力使用 ContextDependentView 尝试为其添加重载。我收到一条错误消息,说没有 ContextDependentView 的重载方法需要 1 个参数。我的原始代码是这样的

// This worked fine
return View(new ModelSample { getInfo= info, Retrieving= Retrieve })

// This is now what I have tried to do that doesn't work
return ContextDependentView(new ModelSample { getInfo= info, Retrieving= Retrieve })

//This is the method for ContextDependentView()


private ActionResult ContextDependentView()
    {
        string actionName = ControllerContext.RouteData.GetRequiredString("action");
        if (Request.QueryString["content"] != null)
        {

            ViewBag.FormAction = "Json" + actionName;
            return PartialView();
        }
        else
        {
            ViewBag.FormAction = actionName;
            return View();
        }
    }

我显然看到没有重载,但是如何向 ContextDependentView 方法添加重载以接受我的模型,例如 return View()..thanks

【问题讨论】:

    标签: asp.net-mvc-4 model extension-methods


    【解决方案1】:

    将此重载添加到您的控制器:

    private ActionResult ContextDependentView(SampleModel model)
    {
        string actionName = ControllerContext.RouteData.GetRequiredString("action");
        if (Request.QueryString["content"] != null)
        {
    
            ViewBag.FormAction = "Json" + actionName;
            return PartialView();
        }
        else
        {
            ViewBag.FormAction = actionName;
            return View();
        }
    }
    

    应该可以的...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多