简体   繁体   English

如何将ViewBag从控制器传递到View(Ajax Url)

[英]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.

相关问题 如何将Ajax URL中的两个变量传递给codeigniter控制器? - How Can I pass two variable in Ajax URL to codeigniter controller? 如何在ASP.NET MVC 5中将我的viewbag内容(一个巨大的表)从视图传递到控制器 - how to pass my viewbag content (a huge table) from view to controller, in ASP.NET MVC 5 如何将值从XML视图传递给控制器​​? - How can I pass a value from an XML view to the controller? 如何使用URL中的jQuery ajax将变量传递给控制器 - how to pass a variable in to controller using jquery ajax from url 将数组从JavaScript传递到viewbag并在控制器中使用 - pass array from JavaScript to viewbag and use it in controller 如何在Codeigniter中使用Ajax将变量从Controller传递到View? - How to pass variable from Controller to View with Ajax in Codeigniter? 如何使用rails中的ajax将数据从控制器传递到视图 - How to pass data from controller to view with ajax in rails 如何将数据从ajax传递到控制器,然后重新加载以查看页面 - how to pass data from ajax to controller then reload to view page 如何使用Ajax将模型从视图传递到控制器 - How to pass model from view to controller using ajax 如何在没有Ajax请求的情况下将多个参数从视图传递到控制器 - How to pass multiple parameter from view to controller without ajax request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM