Calling another different view from the controller using ASP.NET MVC 4

后端 未结 5 1738
夕颜
夕颜 2020-12-05 03:58

I have a view (Index.cshtml) with a submit button. When the submit button is clicked, it calls an action (Action01) within the controller (TestController.cs) so at the end o

相关标签:
5条回答
  • 2020-12-05 04:32

    To return a different view, you can specify the name of the view you want to return and model as follows:

    return View("ViewName", yourModel);
    

    if the view is in different folder under Views folder then use below absolute path:

    return View("~/Views/FolderName/ViewName.aspx");
    
    0 讨论(0)
  • 2020-12-05 04:35

    You can directly return a different view like:

    return View("NameOfView", Model);
    

    Or you can make a partial view and can return like:

    return PartialView("PartialViewName", Model);
    
    0 讨论(0)
  • 2020-12-05 04:39

    Also, you can just set the ViewName:

    return View("ViewName");
    

    Full controller example:

    public ActionResult SomeAction() {
        if (condition)
        {
            return View("CustomView");
        }else{
            return View();
        }
    }
    

    This works on MVC 5.

    0 讨论(0)
  • 2020-12-05 04:40
                public ActionResult Index()
                {
                    return View();
                }
    
    
                public ActionResult Test(string Name)
                {
                    return RedirectToAction("Index");
                }
    

    Return View Directly displays your view but

    Redirect ToAction Action is performed

    0 讨论(0)
  • 2020-12-05 04:44

    You have to specify the name of the custom view and its related model in Controller Action method.

    public ActionResult About()
    {            
       return View("NameOfViewYouWantToReturn",Model); 
    }
    
    0 讨论(0)
提交回复
热议问题