Action参数和View、Json、重定向

时间:2023-03-09 17:11:34
Action参数和View、Json、重定向

一、Action

1、Action参数: 普通参数、Model类、FormCollection

(1)、普通参数 Index(string name,int age)   框架会自动把用户请求的QueryString 或者Post表单中的值根据参数名字映射对应参数的值,适用于查询参数比较少的情况。

        public ActionResult F3(string name, int age)
{
return Content("姓名:" + name + "年龄:" + age);
}

(2)、Model类: 这种类叫ViewModel

        public ActionResult Index(IndexModel model)
{
return View(model);
}

(3)、 FormCollection ,采用fc["name"]这种方式访问,适用于表单元素不确定的情况,用的比较少。

        public ActionResult F2Show()
{ return View();
}
public ActionResult F2(FormCollection fc)
{
string name = fc["name"];
string age = fc["age"];
return Content("姓名:" + name + "年龄:" + age);
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>F2Show</title>
</head>
<body>
<form action="~/Test/F2" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

(4)  一部分是普通参数,一部分Model:

        public ActionResult Index(IndexModel model,string department)
{
return Content(model.Num1+model.Num2+department);
}

(5) 添加默认值,默认值参数在最后

        public ActionResult F3(string name, int age=)
{
return Content("姓名:" + name + "年龄:" + age);
}

2、 Action 方法不能重载,除了加上[HttpGet] 、[HttpPost] 标记

        [HttpGet]
public ActionResult F4()
{ return View();
} [HttpPost]
public ActionResult F4(string name,int age)
{ return Content("姓名:" + name + "年龄:" + age);
}
<body>
<form action="~/Test/F4" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="submit" />
</form>
</body>

二、View

1.

        public ActionResult F2Show()
{
// return View();
return View("F2"); //找F2.cshtml显示
}

2、 显示View的时候传递Model

        public ActionResult F2Show()
{
IndexModel model = new IndexModel();
model.Num1 = ;
return View("F2",model); //第二个参数是Model
}
        public ActionResult F2Show()
{
string name = "wang";
return View("F2",(object)name); //第二个参数是Model
} //cshtml页
@model string

二、ActionResult

1、 View()  是一个方法,返回值是ViewResult 类型,ViewResult继承自ActionResult

三、Json

JsonResult Json(object data) 把data对象序列化成json字符串返回给客户端,并且设置 contentType为“application/json”.

json 方法是默认禁止Get请求的(主要是为了防止CSRF攻击,ajax请求无法跨域),如果需要调用Get请求: return Json(p1,JsonRequestBehavior.AllowGet)

        public ActionResult JsonTest1()
{
var p1 = new { Name = "chen", Age = };
return Json(p1); //默认Post请求 } public ActionResult J1()
{
return View();
}
    <script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
$("#btn1").click(function () {
$.ajax({
url: "/Test/JsonTest1",
type: "post",
datatype: "json",
success: function (obj) {
alert(obj.Name)
},
error: function () {
alert("error")
} })
}
)
}
)
</script>
      <button id="btn1">点我</button>

json缺点:

1、 日期类型转化成的字符串是“/Date(1532585581810)/”这样格式的,在客户端要用js代码格式化处理麻烦;

2、 json字符串中属性的名字和C#中的大小写一样,不符合js中“小写开头,驼峰命名”的习惯;

四、重定向

1、 Redirect(url);

2、RedirectToAction(string actionName,string  controllerName ):其实最终还是拼接成Url,调用Redirect()

3、 相对路径:

      "." -- 代表目前所在的目录,相对路径。
".." -- 代表上一层目录,相对路径。
"../../" -- 代表的是上一层目录的上一层目录,相对路径。

./ 表示在当前路径下,

../表示在当前路径的上一级路径下.

~/表示当前网站的根目录下.

4、Redirect 和return View的区别:

(1) Redirect 是让浏览器重定向到新的地址,retun view是让服务器把指定的cshtml的内容运行渲染后给到浏览器;

(2) Redirect 浏览器和服务器之间发生了两次交互,return view浏览器和服务器之间发生了1次交互;

(3) Redirect是两次请求,所以第一次设置的ViewBag等这些信息,在第二次是取不到的,而View则是在同一个请求中,所以ViewBag信息取不到;

(4)   如果用Redirect ,则由于是新的对Controller/Action 的请求,所以对应的Action会被执行到,如果是view,则直接拿某个view去显示,对应的Action是不执行的

(5) 什么情况用view?服务器端产生数据,想让一个view去显示,对应的Action是不执行的;

什么情况用Redirect? 让浏览器去访问另外一个页面的时候;

五、其他:

1、 TempData:

一般用于验证码,一次取了之后,在取就没有了

        public ActionResult T1()
{
TempData["code"] = "";
return View();
} public ActionResult TempData1()
{
string code = (string)TempData["code"];
return Content("code=" + code);
}

2、 进行MVC开发的时候,尽量使用****base类,不要用asp.net内核源生的类;这两个类之间没有继承的关系;