zoukankan      html  css  js  c++  java
  • MVC控制器返回值

      public ActionResult Index(string id)//主页 //参数string searchString 访问方式为index?searchString=xxxx 。参数string id 访问方式为index/x
            {
                string searchString = id;
                    //return View(db.Books.ToList()); //返回一个对象集合
                var s = from m in db.Books select m; //查询所有数据            
                if (!string.IsNullOrEmpty(searchString)) //判断传来的数据是否位空
                {
                    s = s.Where(x => x.BookName.Contains(searchString)); //模糊查询数据
                }
                return View(s);
            }
    public ActionResult Edit(int? id) //只能接受整型数据;其他默认null { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest);//传递过去400 //返回400页面 } Book book = db.Books.Find(id); //在books表中查找指定id的对象 赋值给Book对象 if (book == null) { return HttpNotFound(); //未找到调用HttpNotFound()方法,传递 NotFound = 404,返回404 页面 } return View(book); //返回这个对象 }
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include = "BookID,BookName,Author,Price,dt")] Book book)  //编辑
            {
                if (ModelState.IsValid)
                {
                    db.Entry(book).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(book);
            }
            [HttpPost, ActionName("Delete")] //重命名方法名 只接受post请求
            [ValidateAntiForgeryToken]
            public ActionResult DeleteConfirmed(int id)  //删除
            {
                Book book = db.Books.Find(id); //根据指定ID查找
                db.Books.Remove(book); //移除对象
                db.SaveChanges();  //保存修改
                return RedirectToAction("Index"); //返回主页
            }
       public class HomeController : Controller
        {
            // GET: Home
            public ActionResult Index()
            {
                return View();// 参数可以返回model对象  
            }
            //返回ViewResult视图结果,将视图呈现给网页
            public ActionResult A1()
            {
                return View();// 参数可以返回model对象  
            }
            //返回PartialViewResult部分视图结果,主要用于返回部分视图内容
            public ActionResult A2()
            {
                return PartialView("ViewUserControl");//在View/Shared目录下创建ViewUserControl.cshtml部分视图
            }
            //返回ContentResult用户定义的内容类型 
            public ActionResult A3()
            {
                return Content("指定文本", "text/html"); // 可以指定文本类型  
            }
            //返回JsonResult序列化的Json对象
            public ActionResult A4()
            {
                //Dictionary<string, object> dic = new Dictionary<string, object>();
                //dic.Add("id", 100);
                //dic.Add("name", "hello");
                List < string> list= new List<string>();
                list.Add("xxxx");
                list.Add("YYYY");
                list.Add("ZZZZ");
                //["xxxx","YYYY","ZZZZ"]
                return Json(list, JsonRequestBehavior.AllowGet);//若要使用GET请求设置参数为AllowGet
                //{"id":100,"name":"hello"}
            }
            //返回JavaScriptResult可在客户端执行的脚本
            public ActionResult A5()
            {
                string str = string.Format("alter('{0}');", "弹出窗口");
                return JavaScript(str);
            }
            //返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能
            public ActionResult A6()
            {
                string fileName = "~/Content/test.zip"; // 文件名  
                string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名  
                return File(fileName, "application/octet-stream", downFileName);
            }
            // 返回Null或者Void数据类型的EmptyResult 
            public ActionResult A7()
            {
                return null;
            }
    
            //重定向方法:Redirect / RedirectToAction / RedirectToRoute
           //Redirect:直接转到指定的url地址
            public ActionResult Redirect()
            {
                // 直接返回指定的url地址  
                return Redirect("http://www.baidu.com");
            }
            // RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName;也可以带参数
            public ActionResult RedirectResult()
            {
                return RedirectToAction("Index", "Home", new { id = "100", name = "liu" });
            }
            //RedirectToRoute:指定路由进行跳转 //Default为global.asax.cs中定义的路由名称
            public ActionResult RedirectRouteResult()
            {
                return RedirectToRoute("Default", new { controller = "Home", action = "Index" });
            }
        }
  • 相关阅读:
    cookie会话技术
    Vue实现任务列表效果
    Vue实现选项卡效果
    数组API(2)
    数组常用API(1)
    sticky,粘性定位
    了解HTTP协议和TCP协议
    快速排序
    冒泡排序
    【译】x86程序员手册21-6.3.5为操作系统保留的指令
  • 原文地址:https://www.cnblogs.com/enych/p/8252773.html
Copyright © 2011-2022 走看看