0

on a button click I want to pass value to Action method of type HttpGet and then display it on cshtml

Ex:Controller method

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

    return View(receiptBankUserList);
}
Stefan Over
  • 5,851
  • 2
  • 35
  • 61
NiksJ
  • 401
  • 1
  • 4
  • 7

1 Answers1

0

Here is some example how to do this:

Controller:

[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();
        }
    }
}

Method to render view as string is from 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); 
    });
});
Community
  • 1
  • 1
Sousuke
  • 1,203
  • 1
  • 15
  • 25