mvc如何使用JavaScript在view和controller之间传递数据
来源:程序员人生 发布时间:2015-06-09 08:25:46 阅读次数:3432次
在1般的BS项目开发中,我们常常需要在前台和后台之间传递数据,mvc架构中,也需要在view和controller之间传递数据,那末我们应当如何实现呢?
view想controller传递数据:view中代码
<li>@Html.ViewComponent().WfSaveLink(Model).Id("saveToLink").OnBeforeDo("externalData")</li>
<scriptlanguage="javascript" type="text/javascript">
var externalData = function () {
var state =$("#listItem").val();
var explain =$("#TextArea1").val();
if (CheckDataIsNotEmpty(state)&& CheckDataIsNotEmpty(explain)) {
$.ajax({
type: "post",
url:"@Url.RouteUrl("workflowOperation", new { action ="UpdateModel", controller = "SignQuote" })",
data:
{
state: state,
explain: explain,
}
});
}
};
</script>
controller中代码:
Stringstate = QueryString["State"];
Stringexplain = QueryString["explain"];
controller想view中传递数据:
controller数据:
public ActionResultsendRead(string resourceID)
{
string msg = "数据测试";
return Json(msg,JsonRequestBehavior.AllowGet);
}
view中代码:
<scriptlanguage="javascript" type="text/javascript">
var sendReadInfo = function () {
$.ajax({
type: "GET",
url:"@Url.RouteUrl("workflowOperation", new { action ="sendRead", controller = "SignQuote" })" +"?resourceID=@Model.Data.ID",
success: function (s) {
if (s) {
alert(s)
window.location =window.location;
}
}
});
}
</script>
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠