ASP.NET MVC的一些知识点
1.前台向后台页面传值
(1)ajax方式
$.ajax({ type:'POST', // 规定请求的类型(GET 或 POST) url:uploadV, // 请求的url地址 dataType:'json', //预期的服务器响应的数据类型 data:{},//规定要发送到服务器的数据 beforeSend:function(){ //发送请求前运行的函数(发送之前就会进入这个函数) // .... }, success: function(result){ // 当请求成功时运行的函数 //... }, error:function(result){ //失败的函数 //... }, complete:function(){ //请求完成时运行的函数(在请求成功或失败之后均调用,即在 success 和 error 函数之后,不管成功还是失败 都会进这个函数) // ... } });
(2)通过form表单提交方式
<form action="~\HookPlan\Index" method="post" oninput="x.value = parseInt(firstinput.value) + parseInt(secondinput.value)"> <p>FirstNumber:</p> <input type="number" id="firstinput" name="first_input" placeholder="FirstNumber" /><br /> <p>SecondNumber:</p> <input type="number" id="secondinput" name="second_input" placeholder="SecondNumber" /><br /> <input type="submit" value="提交" id="input_sub" /><br /> <output name="x" for="firstinput secondinput"> </output> </form>
第一种建立实体类
public IActionResult Index(Test_Number collection) { var number1 = collection.first_input; var number2 = collection.second_input; Console.WriteLine(int.Parse(number1) +int.Parse(number2)); }
第二种直接参数形式
public IActionResult Index(string first_input,string second_input) { Console.WriteLine(int.Parse(first_input) +int.Parse(second_input)); }
第三种FormCollection方式
public ActionResult GetUserInfo(FormCollection collection) { string username = collection["first_input"]; string password = collection["second_input"]; }
2.后台向前台页面传值
public class Products { public int Id { get; set; } public string Name { get; set; } public double Price { get; set; } }
var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 };
(1)第一种方式ViewData
ViewData["person"] = p;
(2)第二种方式ViewBag
ViewBag._Product = p;
(3)第三种方式
public ActionResult Index() { var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 }; return View(p); }
前台
@using MvcTest.Models; @model Products @{ ViewBag.Title = "Index"; } <h1>@Model.Id</h1> <h2>@Model.Name</h2> <h3>@Model.Price</h3>
(4)第四种方式TempData
public ActionResult Index() { var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 }; TempData["_product"] = p; return RedirectToAction("Order"); } public ActionResult Order() { return View(); }
前台
@{ Products p = (Products)TempData["_product"]; }
这种方式只能传递1次
3.页面跳转的方式
(1)js方式
window.location='/Blog/Index
(2)c#方式
RedirectToAction()
(3)HTML辅助器模式
@Html.ActionLink("nihao", "Here")