Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何让MVC3调用具有不同名称的视图?_C#_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

C# 如何让MVC3调用具有不同名称的视图?

C# 如何让MVC3调用具有不同名称的视图?,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我的控制器中有以下各项: public ActionResult Tests() { } public ActionResult Test() { } 当我返回时,我希望两者都转到Test.cshtml视图。有人能告诉我这是否可行吗。我知道Test-one默认会转到Test.cshtml,但是Tests呢?如何将其指向Test.cshtml 或者,我应该将它们保留为两个视图并使用RenderPartial吗?如果我这样做了,那么如何将模型传递

我的控制器中有以下各项:

    public ActionResult Tests()
    {
    }

    public ActionResult Test()
    { 
    }
当我返回时,我希望两者都转到Test.cshtml视图。有人能告诉我这是否可行吗。我知道Test-one默认会转到Test.cshtml,但是Tests呢?如何将其指向Test.cshtml

或者,我应该将它们保留为两个视图并使用RenderPartial吗?如果我这样做了,那么如何将模型传递到RenderPartial视图中

谢谢,

返回时请提供视图名称 似乎您还没有真正签出方法重载。从控制器操作返回时,有返回ViewResult的命令。其中之一允许您提供视图名称:

public ActionResult Tests()
{
    ...
    // provide the model too if you need to
    return View("Test", model);
}

public ActionResult Test()
{ 
    ...
    // provide the model too if you need to
    return View("Test", model);
}
return View("Test");
Html.RenderPartial也有重载 Html.RenderPartial也是如此,您可以在调用模型时提供模型。检查其扩展方法

我建议您查看文档,因为您将了解更多。返回时请提供视图名称 似乎您还没有真正签出方法重载。从控制器操作返回时,有返回ViewResult的命令。其中之一允许您提供视图名称:

public ActionResult Tests()
{
    ...
    // provide the model too if you need to
    return View("Test", model);
}

public ActionResult Test()
{ 
    ...
    // provide the model too if you need to
    return View("Test", model);
}
return View("Test");
Html.RenderPartial也有重载 Html.RenderPartial也是如此,您可以在调用模型时提供模型。检查其扩展方法

我建议您查看文档,因为您将了解更多。使用重载

这将返回名为Test的视图,尽管调用了操作

这将返回名为Test的视图,尽管调用了操作

视图重载允许您显式命名要使用的视图

public ActionResult Tests()
{
  return View("Test");
 }

 public ActionResult Test()
 { 
   return View("Test");
 }
视图重载允许您显式命名要使用的视图

public ActionResult Tests()
{
  return View("Test");
 }

 public ActionResult Test()
 { 
   return View("Test");
 }

是,只需指定视图名称:

public ActionResult Tests()
{
    ...
    // provide the model too if you need to
    return View("Test", model);
}

public ActionResult Test()
{ 
    ...
    // provide the model too if you need to
    return View("Test", model);
}
return View("Test");

是,只需指定视图名称:

public ActionResult Tests()
{
    ...
    // provide the model too if you need to
    return View("Test", model);
}

public ActionResult Test()
{ 
    ...
    // provide the model too if you need to
    return View("Test", model);
}
return View("Test");

最好总是指定视图的名称,这样在进行单元测试时,您可以检查它以确保从控制器返回预期视图。最好总是指定视图的名称,这样在进行单元测试时,您可以检查它以确保从控制器返回预期视图。抱歉。我不确定,因为我使用的是viewmodel。目前是return Viewtest和return Viewtests。我如何适应您对viewmodel的建议?谢谢。@DavidG:按照建议检查重载,您会发现您也可以提供模型。我编辑了我的代码。对不起。我不确定,因为我使用的是viewmodel。目前是return Viewtest和return Viewtests。我如何适应您对viewmodel的建议?谢谢。@DavidG:按照建议检查重载,您会发现您也可以提供模型。我编辑了我的代码。对不起,我没有解释清楚。我使用ViewModel。它可以是一样的。如何调用视图名称并在返回中指定viewmodel?谢谢,很抱歉没有在问题中说这个。很抱歉我解释得不好。我使用ViewModel。它可以是一样的。如何调用视图名称并在返回中指定viewmodel?谢谢,很抱歉没有在问题中这样说。