在按钮上单击我想将值传递给HttpGet类型的Action方法,然后在cshtml上显示它

时间:2016-02-11 10:42:42

标签: asp.net-mvc

单击按钮我想将值传递给HttpGet类型的Action方法,然后在cshtml上显示

Ex:控制器方法

[HttpGet]
public ActionResult GetMandateListForCustomer(int userId, int customerId)
{
    var receiptBankUserList = new CustomerMandateReport()
    {
        GetMandateListForCustomer = _mandateManager.GetMandateListForCustomer(userId, customerId)
    };

    return View(receiptBankUserList);
}

1 个答案:

答案 0 :(得分:0)

以下是如何执行此操作的示例:

控制器:

[HttpGet]
public string GetMandateListForCustomer(int userId, int customerId)
{
    var receiptBankUserList = new CustomerMandateReport()
    {
        GetMandateListForCustomer = _mandateManager.GetMandateListForCustomer(userId, customerId)
    };
    return RenderRazorViewToString("ViewName", receiptBankUserList);
}

// use this method to change your view into string
public string RenderRazorViewToString(string viewName, object model)
{
    ViewData.Model = model;
    using (var sw = new StringWriter())
    {
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);
        viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
        return sw.GetStringBuilder().ToString();
        }
    }
}

将视图呈现为字符串的方法来自here

的Javascript

$('#btnAddElement').click(function () {
    $.get("UrlToAction?userIDd=123&customerId=321", function (data) {
        // replace element with class result with
        // our new html which we get from controller
        $(".result").html(data); 
    });
});