如何将数据库中的数据保存到控制器

时间:2012-10-11 07:08:33

标签: c# jquery asp.net-mvc

如何将数据库中的数据保存到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将整个数据保存在数据库中。

1 个答案:

答案 0 :(得分: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);
}