先后台相互传值的方法概述

前台向后传值:
1.用网站网址自带参数向后传【http://localhost:5809/ProductInfo/CatogryProducts?categoryID=0021】,后台controller用string ID = Request.QueryString["categoryID"]接收,其中categoryID是参数名称。html

2.ajax传值,其中$.post(url,data,success(data, textStatus, jqXHR),dataType)ajax

参数     描述
url     必需。规定把请求发送到哪一个 URL。
data     可选。映射或字符串值。规定连同请求发送到服务器的数据。
success(data, textStatus, jqXHR)     可选。请求成功时执行的回调函数。
dataType     可选。规定预期的服务器响应的数据类型。默认执行智能判断(xml、json、script 或 html)。json

该函数是简写的 Ajax 函数,等价于:服务器

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});asp.net

$.get()也差很少。百度函数

3.表单传值,表单元素的name属性必须和后台model类型同样,直接用一个对象接收。在form的action属性中引用controller的方法就能够post

 

ViewData ViewBag
它是Key/Value字典集合 它是dynamic类型对像
从Asp.net MVC 1 就有了 ASP.NET MVC3 才有
基于Asp.net 3.5 framework 基于Asp.net 4.0与.net framework
ViewData比ViewBag快 ViewBag比ViewData慢
在ViewPage中查询数据时须要转换合适的类型 在ViewPage中查询数据时不须要类型转换
有一些类型转换代码 可读性更好

 

4.viewdata传值  网站

控制器向视图中传值ViewData详解url

  1.将一个字符串传值到视图中spa

         在action中咱们将字符串保存在ViewData(或ViewBag [asp.net 3或以上才可用])中代码以下:

         public ActionResult Index()
        {
            ViewData["str1"]= "这是一个字符串";

             //也能够使用ViewBag来传递值

            ViewBag.str2="这是另一个字符串";

            return View();
        }

        在视图中咱们能够用下面代码将字符串显示出来

        <h1>@ViewData["str1"]</h1>

        <h1>@ViewBag.str2</h1>

     2.将一个字符串集合传递到视图中

        public ActionResult Index()
        {
           List<string> str1= new List<string>();
            str1.Add("1111");
            str1.Add("2222");
            str1.Add("3333");
            ViewData["str"] = str1;

            return View();
        }

        在视图中咱们经过下面语句将str1的值显示出来

       @foreach (var a in ViewData["str"] as List<string>)
         {
           @a
         }

       3.将一个datatable的值传递到视图中

           public ActionResult Index()
            {

            DataTable newtable = new DataTable("d");
            newtable.Columns.Add("商品编号", typeof(string));
            newtable.Columns.Add("客户编号", typeof(string));
            DataRow NewRow = newtable.NewRow();
            NewRow["商品编号"] = "132323213434";
            NewRow["客户编号"] = "344223443244";
            newtable.Rows.Add(NewRow);
            DataRow SNewRow = newtable.NewRow();
            SNewRow["商品编号"] = "343432445456";
            SNewRow["客户编号"] = "454523432453";
            newtable.Rows.Add(SNewRow);
            ViewData["dt"]= newtable;
            return View();
            }

            在视图中咱们经过下面语句将dt的值显示出来

            注意:在顶部要先加上:@using System.Data;

            <ul>
            @foreach(DataRow dr in (ViewData["dt"] as DataTable).Rows)
               {
                 <li>
                 @dr["商品编号"],@dr["客户编号"],
                 </li>
                }
              </ul>

而后是ViewBag:

public ActionResult UsingViewBag()
{

    ViewBag.Title = " Using ViewBag";
    ViewBag.ProjectName = "My Test Project";
    ViewBag.ProjectDescription = "This is Test Project to demo Viewdata and viewbag details";
    ViewBag.StartDate = new DateTime(2011, 1, 1);
    ViewBag.TotalPrice = 1000;
    ViewBag.TotalDays = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client", "Mr.  Client");
    stackholder.Add("Manager", "Mr. Joy");
    stackholder.Add("Team Leader", "Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewBag.stackholder = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewBag.modules = modules;
    return View();
}


对应View UsingViewBag 的cshtml的ViewBag:

<h1>@ViewBag.Title</h1>
 <div>
   <div>
    <h2>Project Name : @ViewBag.ProjectName</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewBag.ProjectDescription.</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewBag.stackholder )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewBag.modules )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") <br />
     Project TotalPrice: @ViewBag.TotalPrice  ₹  <br />
     Project TotaDays  : @ViewBag.TotalDays 
   </div>
 </div>

 

在controll用ViewBag传一个 List<ProductInfo> list的列表中:

  public ActionResult Index()
        {
            List<ProductInfo> list = new List<ProductInfo>();
            list.Add(new ProductInfo
            {
                Department = "1111aa",
                Describe = "1111aa",
                Brand = "1111aa",
                CategoryID = "1111aa",
                ProductID = "1111aa",
                Color = "1111aa",
                Cost = "1111aa",
                Note = "1111aa",
                Owner = "1111aa",
                PriceTag = "1111aa",
                ProductName = "98uy",
                ShelfLife = "1111aa",
                Size = "1111aa",
                Style = "1111aa",
            });
            ViewBag.list = list;
            return View();
        }

在前台中: @foreach(var item in ViewBag.list){     @item.ProductName;     @item.Department; }

相关文章
相关标签/搜索