WEB开发网
开发学院WEB开发ASP.NET ASP.NET MVC学习笔记-Controller的ActionResult 阅读

ASP.NET MVC学习笔记-Controller的ActionResult

 2009-11-20 16:52:29 来源:WEB开发网   
核心提示:1. 返回ViewResult view plaincopy to clipboardPRint?public ActionResult Index() { ViewData["Message"] = "Welcome to asp.net MVC!"; return View(
1. 返回ViewResult

view plaincopy to clipboardPRint?
public ActionResult Index()  
{  
  ViewData["Message"] = "Welcome to asp.net MVC!";  
  return View();  
} 
    public ActionResult Index()
    {
      ViewData["Message"] = "Welcome to ASP.NET MVC!";
      return View();
    }

ActionResult是ViewResult基类

2.返回字符串

view plaincopy to clipboardprint?
public string GetData(string data)  
{  
  string str = "Return String";  
  return str;  
} 
    public string GetData(string data)
    {
      string str = "Return String";
      return str;
    }

3.返回ContentResult

view plaincopy to clipboardprint?
public ContentResult ContentString()  
{  
  return Content("This is Content Result Sample");  
} 
    public ContentResult ContentString()
    {
      return Content("This is Content Result Sample");
    }

4.返回PartialViewResult


view plaincopy to clipboardprint?
public PartialViewResult PartiviewSample()  
{  
  return PartialView();  
} 
    public PartialViewResult PartiviewSample()
    {
      return PartialView();
    }

5.返回javascript

view plaincopy to clipboardprint?
    public JavascriptResult JSResult()  
    {  
      string js=@"<mce:script language='javascript'><!--  
              alert('This is javascript result sample);  
                
// --></mce:script>";  
      return JavaScript(js);  
    } 
    public JavaScriptResult JSResult()
    {
      string js=@"<mce:script language='javascript'><!--
              alert('This is javascript result sample);
              
// --></mce:script>";
      return JavaScript(js);
    }

6. 返回JSON字符串

view plaincopy to clipboardprint?
public JsonResult JsonString()  
{  
   return Json(new{name="name", Sex="Sex"});  
} 
    public JsonResult JsonString()
    {
      return Json(new{name="name", Sex="Sex"});
    }

7.返回FileResult

view plaincopy to clipboardprint?
public FileResult FileSample()  
{  
  return File(  
    "/Content/site.CSS",     //文件路径  
     "text/css"           //文件类型  
    );  
} 

Tags:ASP NET MVC

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接