[英]How can I pass a ViewBag from controller to View(Ajax Url)
** Controller Code ** **控制器代码**
I have the below code in my Controller
and the method get_curent_LoanNumber
我的
Controller
和方法get_curent_LoanNumber
包含以下代码
public JsonResult (string loan_code)
{
try
{
using (sqlConnection)
{
dynamic loanno = sqlConnection.Query("get_loan_number", new {loan_code },
commandType: CommandType.StoredProcedure).SingleOrDefault();
ViewBag.currentLoanNumbe= loanno.LoanNo;
return Json(loanno, JsonRequestBehavior.AllowGet);
}
}
catch (Exception)
{
throw;
}
}
View Code This is the code in the View
, the JavaScript
function savePayslipInfo
makes the Ajax
request. 查看代码这是代码
View
中, JavaScript
功能savePayslipInfo
使Ajax
请求。
function () {
console.log('@ViewBag.currentLoanNumbe');
$.ajax({
type: "POST",
url: "http://localhost:1079/loanapplication/save_Payslip_Info/?loan_no" + '@ViewBag.currentLoanNumbe',
data: {
//loan_no: $(loan_no).val(),
basic_salary: $("#basic_salary").val(),
house_allowance: $("#house_allowance").val(),
other_allowance: $("#other_allowance").val(),
other_payment: $("#other_payment").val(),
total_deduction: $("#total_deduction").val()
},
success: function () {
$('#msg').html("Payslip info saved successfully").fadeIn('slow');
$('#msg').delay(4000).fadeOut('slow');
}
});
}
This is not how MVC works. 这不是MVC的工作方式。 You are not returning a view from your action method.
您没有从操作方法返回视图。 You are returning a JSON.
您正在返回JSON。 So there is no view that gets rendered tied to the action method.
因此,没有渲染视图绑定到action方法。 What you are essentially doing is using the action method as an API and calling it from Javascript using AJAX which is not the same as returning and binding a view from action.
您实际上要做的是将action方法用作API,并使用AJAX从Javascript调用它,这与从action返回和绑定视图不同。 Your ViewBag won't be accessible because the View you are using here is not being rendered by the Action method in the question.
您的ViewBag将无法访问,因为问题中的Action方法未呈现您正在使用的View。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.