先後臺相互傳值的方法概述

前臺向後傳值:
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; }

相關文章
相關標籤/搜索