0

How to save Data In Database to the controller.i am using jquery to get data from the textbox. please help me.

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;

            }
        });
    });

i am getting data to the textbox using jquery. now i want to save the whole data in the database through Controller.

4

1 回答 1

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);
}
于 2012-10-11T07:21:35.310 回答