2012-10-11 101 views
0

如何將數據庫中的數據保存到controller.i使用jquery從文本框中獲取數據。請幫幫我。如何將數據庫中的數據保存到控制器

Contactcontroller.cs

public ActionResult Create(string Name, string Company, string 
Regarding, string Email, string Phone, string Message)   { 
      string body = "Name: " + Name + "<br>" + "\nCompany: " + Company + "<br>" + "Regarding: " + Regarding + "<br>" + "Email: " + 
     Email + "<br>" + "Phone: " + Phone + "<br>" + "Message: " + Message; 
        try 
    { 
    // code 
    } 
    catch 
    { 
} 

的jquery.js

$("#btnSubmit").click(function (event) { 
var data = { Name: $("#txtbxName").val(), Company: $("#txtbxCompany").val(), Regarding: 
      $("#ddlRegarding").val(), Email: $("#txtbxEmail").val(), Phone: $("#txtbxPhone").val(), Message: 
      $("#txtarMessage").val() 
     } 
     $.ajax({ 
      type: "POST", 
      url: "/Contact/Create", // the method we are calling 
      contentType: "application/json; charset=utf-8", 
      data: JSON.stringify(data), 

      dataType: "html", 
      success: function (result) { 

       $("#txtarMessage").val(""); 
       alert(result); 
       // Or if you are returning something 
       alert('I returned... ' + result.WhateverIsReturning); 
      }, 
      error: function (result) { 

       alert('Thanks for sending info'); 
       location.reload(); 

       return false; 

      } 
     }); 
    }); 

我使用jQuery的獲取數據到文本框。 現在我想通過Controller將整個數據保存在數據庫中。

+0

困惑,你試圖建構從DB數據視圖?如果你是那麼你真的需要閱讀mvc教程。 – garfbradaz

+0

你的代碼看起來不像'javascript'或'php'或'jquery'。 – Nemoden

+0

你的代碼是.net cs文件並且標記是php? – Sender

回答

0

jQuery的

$.post('/Controller/Create', { Name: $('#Name').val(), Company: "Company", Regarding: "", Email: "",Phone: "",Message: ""}, function (data) { 

}); 

控制器動作

public JsonResult Create(string Name, string Company, string Regarding, string Email, string Phone, string Message) 
{ 
string body = "Name: " + Name + "<br>" + "\nCompany: " + Company + "<br>" + "Regarding: " + Regarding + "<br>" + "Email: " + 
    Email + "<br>" + "Phone: " + Phone + "<br>" + "Message: " + Message; 

return Json(body, JsonRequestBehavior.AllowGet); 
} 
相關問題