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 = "", name = "liu" });
}
//RedirectToRoute:指定路由进行跳转 //Default为global.asax.cs中定义的路由名称
public ActionResult RedirectRouteResult()
{
return RedirectToRoute("Default", new { controller = "Home", action = "Index" });
}
}
MVC控制器返回值的更多相关文章
- mvc 各种返回值
一个例子胜过千言万语,直接上代码 SpringMVC的Controller控制器返回值详解 SpringMVC Controller 返回值几种类型 Spring MVC 更灵活的控制 json 返回 ...
- ASP.NET Core Mvc中空返回值的处理方式
原文地址:https://www.strathweb.com/2018/10/convert-null-valued-results-to-404-in-asp-net-core-mvc/ 作者: F ...
- MVC方法返回值数据
ModelAndView的作用以及用法 使用ModelAndView类用来存储处理完后的结果数据,以及显示该数据的视图.从名字上看ModelAndView中的Model代表模型,View代表视图,这个 ...
- Spring MVC controller返回值类型
SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...
- ZendFramework-2.4 源代码 - 关于MVC - View层 - 控制器返回值
<?php class ReturnController extends AbstractActionController { public function returnAction() { ...
- Spring的MVC控制器返回ModelMap时,会跳转到什么页面?
控制器中的方法如下: @RequestMapping("/person/personDisplay") public ModelMap defaultHandler() { Sys ...
- spring mvc ajax返回值乱码
加入如下配置: <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHan ...
- MVC控制器返回一个list 视图接收
控制器 public ActionResult InfoFrame() { List<Users> list = new List<Users>(); if (Session[ ...
- MVC控制器返回重定向操作
注意:在使用Ajax请求后台时是不能在后台重定向的! 解决方案: if (userInfoService.CheckUser(username, psd, out msg)) { , msg = &q ...
随机推荐
- UCloud 的安全秘钥
UCloud 的安全秘钥(困难) 1200ms 262144K 每个 UCloud 用户会构造一个由数字序列组成的秘钥,用于对服务器进行各种操作.作为一家安全可信的云计算平台,秘钥的安全性至关重要.因 ...
- springCloud学习- 路由网关(zuul)
1.zuul简介 1.1.zuul是什么 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性, ...
- Performance Metrics(性能指标1)
Performance Metrics(性能指标) 在我们开始旅行本书之前,我必须先了解本书的性能指标和希望优化后的结果,在第二章中,我们探索更多的性能检测工具和性能指标,可是,您得会使用这些工具和明 ...
- N天学习一个Linux命令之帮助命令:man
前言 工作中每天都在使用常用的命令和非常用的命令,忘记了用法或者参数,都会bing一下,然后如此循环.一直没有真正的系统的深入的去了解命令的用法,我决定打破它.以前看到有人,每天学习一个linux命令 ...
- Intellij Idea 13:重置设置
最近在将Windows下的Idea的设置导入到Mac下的时候,一时手贱,点了全部结果发现悲剧了,所有的快捷键都变成和Windows的一样了.于是我就在CMD+C,V和Ctrl+C,V中不断的进行头脑锻 ...
- footer在最低显示
footer在最低显示 http://stackoverflow.com/questions/585945/how-to-align-content-of-a-div-to-the-bottom
- 【独立开发人员er Cocos2d-x实战 011】Cocos2dx 3.x命令行生成APK具体解释
Cocos2d-x 3.6项目打包生成apk安卓应用文件,搭建安卓环境的步骤有点繁琐.但搭建一次之后,以后就会很快捷! 过程例如以下: 一.下载安卓环境:搭建Android环境须要用到Android ...
- 基于Windows Azure 安装 SharePoint 2010简体中文语言包
在Windows Azure上安装的Windows Server默认是英文版本的,当时安装的SharePoint也是英文版的,为方便使用,决定安装中文的语言包,具体过程如下: 1. 安装 Window ...
- erlang Unicode 处理
最近在使用erlang做游戏服务器,而字符串在服务器编程中的地位是十分重要的,于是便想仔细研究下字符编码,以及erlang下的字符串处理.先从Unicode开始吧.... [Unicode] Unic ...
- Openwrt中luci配置页面cbi小记
先看看network的配置文件: config interface 'loopback' option ifname 'lo' option proto 'static' option ipaddr ...