因业务需求 要讲如下编辑器中带有样式的数据传入数据库保存
第一种方法 json格式传入
$(".privilegezn_page .btn_ok").click(function () {
if (editor.html() == "")
{
alert("优惠使用指南内容不能为空")
} else {
var str = HTMLEncode(editor.html());
console.log(str);
if (($("select")).length > 0) {
var hotelid = $("#hotel option:selected").attr("value");
} else {
var hotelid = $("#hotelid").val();
}
var cz = $("#xg").val();//操作名id
var gnbh = $("#gnbh").val();
var obj = new Object();
obj.text = str;
obj.xgid = hotelid;
function HTMLEncode(text){
text = text.replace(/&/g, "&") ;
text = text.replace(/</g, "'<'") ;
text = text.replace(/>/g, "'>'") ;
return text;
}
obj.gnbh = gnbh;
obj.cz = cz;
console.log(JSON.stringify(obj));
$.ajax({
type: "POST",
url: "/Business/yhsyznxg",
data:{json:JSON.stringify(obj)},
success: function (data) {
var f = $.parseJSON(data);
if (f.yz1) {
alert("添加成功!");
if ($("#yhsyzncx").length > 0) {
$("#yhsyzncx").trigger("click");
} else {
location.replace(location.href);
}
}
else if (f.yz1 == false) {
alert("添加失败!");
location.replace(location.href);
}
if (f.yz2) {
alert("修改成功!");
if ($("#yhsyzncx").length > 0) {
$("#yhsyzncx").trigger("click");
} else {
location.replace(location.href);
}
}
else if (f.yz2 == false) {
alert("修改失败!");
location.replace(location.href);
}
}
});
}
})
后台页面:
//优惠使用指南
[HttpPost]
public ActionResult yhsyznxg(string json)
{
JObject l = (JObject)JsonConvert.DeserializeObject(json);
//数据库为空则添加
int xgid = (int)l["xgid"];
int gnbh = (int)l["gnbh"];
int cz = (int)l["cz"];
string text = (string)l["text"];
string str = text.Replace("'<'", "<"); //解码去除 html中< >
string str1 = str.Replace("'>'", ">");
var cx = db.yhsyzn.FirstOrDefault(u => u.hotelid == xgid);
JObject array = new JObject();
if (cx != null) //修改
{
if (Session["yhid"] != null)
{
int id = (int)Session["yhid"]; bool pd = qxyz.czyz(id, gnbh, cz); //为什么还要查询一遍权限
if (pd)
{
yhsyzn a = db.yhsyzn.FirstOrDefault(u => u.hotelid == xgid);//查询到对应id
a.text = str1;
db.Entry(a).State = System.Data.Entity.EntityState.Modified; //什么更新???
db.SaveChanges();
array["yz2"] = true;
}
else
{
array["yz2"] = false;
}
}
else
{
array["yz2"] = false;
}
}
else //添加
{
if (Session["yhid"] != null)
{
int id = (int)Session["yhid"];
bool pd = qxyz.czyz(id, gnbh, cz); //为什么还要查询一遍权限
if (pd)
{
yhsyzn a = new yhsyzn();
a.hotelid = xgid;
a.text = text;
db.yhsyzn.Add(a);
db.SaveChanges();
array["yz1"] = true;
}
else
{
array["yz1"] = false;
}
}
}
return Content(array.ToString());
}