页面:
@{
Layout = null;
}
<!DOCTYPE html>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<html>
<head>
<meta charset="utf-8"/>
<title>前端发送json,后台实体类接受</title>
</head>
<body>
<input type="button" id="name" value="点击发送" />
<script>
var datas= {
Id: '81E6F804-67AF-4034-BCF5-02AA696C8418',
Name: '遗体搬运服务2',
CateCode: '0102',
Ptype: '0',
Sales: '10',
SortNum: '0',
IsActive: '0',
Memo: encodeURI('<a>hhda</a>'),
StationId: '81E6F804-67AF-4034-BCF5-02AA696C8418'
}
$('#name').on('click', function (obj) {
$.ajax({
url: '@Url.Action("GetDate","Yinshe")',
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data: datas,
type: 'POST',
//traditional: true,
success: function (e) {
alert(e)
},
error: function (e) {
alert('失败')
}
})
})
</script>
</body>
</html>
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CreateId.Controllers
{
public class YinsheController : Controller
{
// GET: Yinshe
public ActionResult Index()
{
return View();
}
public ActionResult GetDate(P_Product obj) {
var hh = obj.Memo;
var res= HttpUtility.UrlDecode(hh);
return Json(res, JsonRequestBehavior.AllowGet);
}
public partial class P_Product
{
#region Primitive Properties
public virtual System.Guid Id
{
get;
set;
}
public virtual string Name
{
get;
set;
}
public virtual string CateCode
{
get;
set;
}
public virtual int Ptype
{
get;
set;
}
public virtual decimal Sales
{
get;
set;
}
public virtual int SortNum
{
get;
set;
}
public virtual int IsActive
{
get;
set;
}
public virtual string Memo
{
get;
set;
}
public virtual System.Guid StationId
{
get;
set;
}
#endregion
}
}
}
方法:在js中对带特殊字符的字符串进行
encodeURI('<a></a>')
在 控制器中得到
这不是我们想要的:
进行
var res= HttpUtility.UrlDecode(hh); 得到
'
<a></a>
'