我有一个很奇怪的问题。问题是这样的......当我尝试将字符串传递给我的控制器动作(.NET MVC)时,这个:
Small Bubble Roll 3/16" x 1400' x 12" Perforated 3/16 Bubbles 1400 Sq Ft Wrap
我检查了控制台,我收到服务器内部错误500 ...为了使事情变得更糟,我已经测试了这个帖子是否适用于不同类型的标题,例如:" super duper amoled TV"或类似的东西,它的工作原理..我怀疑这个问题是因为该字符串包含特殊的字符,如'和" ...
这是post方法本身:
var postData = {
comment: $('#TextArea1').val(),
rating: $('input[name=rating]:checked').val(),
keyword: '@Session["Title"]'
};
首先发布数据,现在发布帖子:
$.post("/Analyze/SaveWatchList", postData)
.done(function (response) {
// do something with the response here...
});
});
现在控制器动作本身:
[ActionName("SaveWatchList")]
[HttpPost]
public ActionResult SaveWatchList(string comment, string rating, string keyword)
{
}
根据我上面显示的标题本身,动作根本没有被触发..相反,我只是在控制台中获得内部服务器错误500 ...
我该如何解决这个问题?
答案 0 :(得分:1)
您可以序列化postData
并期望操作中的请求对象可以进一步反序列化以获取单个属性。
型号:
public class ExampleRequest {
public string comment {get; set;};
public string rating {get; set;};
public string keyword {get; set;};
}
控制器操作:
[ActionName("SaveWatchList")]
[HttpPost]
public ActionResult SaveWatchList(ExampleRequest req)
{
var comment = req.comment;
var rating = req.rating;
var keyword = req.keyword;
}
<强>更新强>
Json对象:
var postData = {
comment: $('#TextArea1').val(),
rating: $('input[name=rating]:checked').val(),
keyword: '@Session["Title"]'
};