【发布时间】:2015-07-22 23:53:40
【问题描述】:
所以我一直在使用 ASP.NET MVC 5 Web 应用程序,并且正在做一个调查式项目。本质上,用户被问到一个问题,我进行了一些格式化,然后我希望将数据传回我的控制器,以便在另一个视图中提供下一个问题。问题是将数据从 javascript 变量传递到 c# 变量(方法)。这是我的代码:
Javascript:
function validateDateInput() {
var year = $('#yearInput').val();
var month = $('#monthInput').val();
var day = $('#dayInput').val();
//a lot of validation in here to make sure the date is an actual date
goToNextQuestion(month + '/' + day + '/' + year);
}
function goToNextQuestion(output) {
//here is where I need to pass my variable, output, to a c# function in my controller
//I need to call the method submitUserAnswer(output)
}
还有我的 C# 代码:
public void submitUserAnswer(String userOutput) {
//here I take the answer and feed the user the next question, possibly linking to other c# methods
}
所以我最初打算使用[WebMethod],但在那里遇到了一些问题(我对 C# 和 ASP.Net 非常陌生,找不到实现它的方法。我不能出于显而易见的原因,只需将变量传递给ViewBag,然后我遇到了一个建议使用jQuery Ajax 调用的帖子。我以前从未使用过Ajax。如何格式化Ajax 调用来做我想做的事,或者是还有另一种更简单的方法来做同样的事情吗?
【问题讨论】:
标签: javascript c# jquery asp.net ajax