1. public ActionResult Index(string id)//主页 //参数string searchString 访问方式为index?searchString=xxxx 。参数string id 访问方式为index/x
  2. {
  3. string searchString = id;
  4. //return View(db.Books.ToList()); //返回一个对象集合
  5. var s = from m in db.Books select m; //查询所有数据
  6. if (!string.IsNullOrEmpty(searchString)) //判断传来的数据是否位空
  7. {
  8. s = s.Where(x => x.BookName.Contains(searchString)); //模糊查询数据
  9. }
  10. return View(s);
  11. }
  12. public ActionResult Edit(int? id) //只能接受整型数据;其他默认null
  13. {
  14. if (id == null)
  15. {
  16. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);//传递过去400 //返回400页面
  17. }
  18. Book book = db.Books.Find(id); //在books表中查找指定id的对象 赋值给Book对象
  19. if (book == null)
  20. {
  21. return HttpNotFound(); //未找到调用HttpNotFound()方法,传递 NotFound = 404,返回404 页面
  22. }
  23. return View(book); //返回这个对象
  24. }
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult Edit([Bind(Include = "BookID,BookName,Author,Price,dt")] Book book) //编辑
  4. {
  5. if (ModelState.IsValid)
  6. {
  7. db.Entry(book).State = EntityState.Modified;
  8. db.SaveChanges();
  9. return RedirectToAction("Index");
  10. }
  11. return View(book);
  12. }
  13. [HttpPost, ActionName("Delete")] //重命名方法名 只接受post请求
  14. [ValidateAntiForgeryToken]
  15. public ActionResult DeleteConfirmed(int id) //删除
  16. {
  17. Book book = db.Books.Find(id); //根据指定ID查找
  18. db.Books.Remove(book); //移除对象
  19. db.SaveChanges(); //保存修改
  20. return RedirectToAction("Index"); //返回主页
  21. }
  1. public class HomeController : Controller
  2. {
  3. // GET: Home
  4. public ActionResult Index()
  5. {
  6. return View();// 参数可以返回model对象
  7. }
  8. //返回ViewResult视图结果,将视图呈现给网页
  9. public ActionResult A1()
  10. {
  11. return View();// 参数可以返回model对象
  12. }
  13. //返回PartialViewResult部分视图结果,主要用于返回部分视图内容
  14. public ActionResult A2()
  15. {
  16. return PartialView("ViewUserControl");//在View/Shared目录下创建ViewUserControl.cshtml部分视图
  17. }
  18. //返回ContentResult用户定义的内容类型
  19. public ActionResult A3()
  20. {
  21. return Content("指定文本", "text/html"); // 可以指定文本类型
  22. }
  23. //返回JsonResult序列化的Json对象
  24. public ActionResult A4()
  25. {
  26. //Dictionary<string, object> dic = new Dictionary<string, object>();
  27. //dic.Add("id", 100);
  28. //dic.Add("name", "hello");
  29. List < string> list= new List<string>();
  30. list.Add("xxxx");
  31. list.Add("YYYY");
  32. list.Add("ZZZZ");
  33. //["xxxx","YYYY","ZZZZ"]
  34. return Json(list, JsonRequestBehavior.AllowGet);//若要使用GET请求设置参数为AllowGet
  35. //{"id":100,"name":"hello"}
  36. }
  37. //返回JavaScriptResult可在客户端执行的脚本
  38. public ActionResult A5()
  39. {
  40. string str = string.Format("alter('{0}');", "弹出窗口");
  41. return JavaScript(str);
  42. }
  43. //返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能
  44. public ActionResult A6()
  45. {
  46. string fileName = "~/Content/test.zip"; // 文件名
  47. string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名
  48. return File(fileName, "application/octet-stream", downFileName);
  49. }
  50. // 返回Null或者Void数据类型的EmptyResult
  51. public ActionResult A7()
  52. {
  53. return null;
  54. }
  55.  
  56. //重定向方法:Redirect / RedirectToAction / RedirectToRoute
  57. //Redirect:直接转到指定的url地址
  58. public ActionResult Redirect()
  59. {
  60. // 直接返回指定的url地址
  61. return Redirect("http://www.baidu.com");
  62. }
  63. // RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName;也可以带参数
  64. public ActionResult RedirectResult()
  65. {
  66. return RedirectToAction("Index", "Home", new { id = "", name = "liu" });
  67. }
  68. //RedirectToRoute:指定路由进行跳转 //Default为global.asax.cs中定义的路由名称
  69. public ActionResult RedirectRouteResult()
  70. {
  71. return RedirectToRoute("Default", new { controller = "Home", action = "Index" });
  72. }
  73. }

MVC控制器返回值的更多相关文章

  1. mvc 各种返回值

    一个例子胜过千言万语,直接上代码 SpringMVC的Controller控制器返回值详解 SpringMVC Controller 返回值几种类型 Spring MVC 更灵活的控制 json 返回 ...

  2. ASP.NET Core Mvc中空返回值的处理方式

    原文地址:https://www.strathweb.com/2018/10/convert-null-valued-results-to-404-in-asp-net-core-mvc/ 作者: F ...

  3. MVC方法返回值数据

    ModelAndView的作用以及用法 使用ModelAndView类用来存储处理完后的结果数据,以及显示该数据的视图.从名字上看ModelAndView中的Model代表模型,View代表视图,这个 ...

  4. Spring MVC controller返回值类型

    SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...

  5. ZendFramework-2.4 源代码 - 关于MVC - View层 - 控制器返回值

    <?php class ReturnController extends AbstractActionController { public function returnAction() { ...

  6. Spring的MVC控制器返回ModelMap时,会跳转到什么页面?

    控制器中的方法如下: @RequestMapping("/person/personDisplay") public ModelMap defaultHandler() { Sys ...

  7. spring mvc ajax返回值乱码

    加入如下配置: <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHan ...

  8. MVC控制器返回一个list 视图接收

    控制器 public ActionResult InfoFrame() { List<Users> list = new List<Users>(); if (Session[ ...

  9. MVC控制器返回重定向操作

    注意:在使用Ajax请求后台时是不能在后台重定向的! 解决方案: if (userInfoService.CheckUser(username, psd, out msg)) { , msg = &q ...

随机推荐

  1. iOS学习笔记18-CoreData你懂的

    一.CoreData介绍 CoreData是iOS5之后新出来的的一个框架, 是对SQLite进行一层封装升级后的一种数据持久化方式. 它提供了对象<-->关系映射的功能,即能够将OC对象 ...

  2. Likecloud-吃、吃、吃(洛谷 1508)

    题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...

  3. 洛谷 P2084 进制转换

    P4122 [USACO17DEC]Blocked Billboard 题目描述 During long milking sessions, Bessie the cow likes to stare ...

  4. django book chapter 2

    Django’s optional GIS (Geographic Information Systems) support requires Python 2.5 to 2.7. 这里提到了djan ...

  5. MFC 小知识总结四

    1 PlaySound  播放WAV格式的音乐 This function plays a sound specified by a file name, resource, or system ev ...

  6. 我的Android进阶之旅------&gt;Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  7. jenkins任务失败重新构建插件Naginator Plugin

    1.下载插件Naginator Plugin 2. 如何配置失败任务自动重试 安装Naginator+Plugin后,新建一个任务,在构建后操作 选择 "Retry build after ...

  8. Hdu oj 1012 u Calculate e

    分析:注意格式. #include<stdio.h> int main() { int i,j,k; double sum=0; printf("n e\n- --------- ...

  9. jsp模板配置

    <%-- Created by IntelliJ IDEA. User: ${USER} Date: ${DATE} Time: ${TIME} To change this template ...

  10. 2017 Multi-University Training Contest - Team 2 &hdu 6055 Regular polygon

    Regular polygon Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...