Reputation: 1021
I want to call a action method in controller. but the actonmethod has no view.
I have this:
<div class="col-lg-6 col-md-8 col-sm-10 ">
<a href="@Url.Action("GeneratePDFFFromHtml", "Product")" class="btn btn-primary enabled"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF </a>
</div>
and this is my action method:
[HttpPost]
public ActionResult GeneratePDFFFromHtml(EditProductModel model, string data)
{
SubmittedForm sf = new SubmittedForm();
string schema = requestSchema;
customer_DbConnection db = new customer_DbConnection();
RenderFormController renderController = new RenderFormController();
renderController.GeneratePdf(data, db,sf);
//return RedirectToAction(model.DesignId, "Prdocut/Edit");
return Content("It works");
}
Upvotes: 3
Views: 7590
Reputation: 35
Thank you Andy Korneyev!!! Your sample code helped me solve something I have been struggling with all day. I used your code (and modified it slightly) to go back after running a code block function without a refresh/view.
return Content("<script type='text/javascript'>window.history.back();</script>");
I know this is really old but hopefully you see this.
Upvotes: 0
Reputation: 1220
Easier way is to use routing, route attribute with your controller as below.
In your controller simply decide the needed route and add it. I've used ImportExport
[Area("Exporting")]
[Route("api/ImportExport")]
public class ImportExportController : Controller
{
............
[Route("GeneratePDF")]
[HttpPost]
public void GeneratePDFFFromHtml(string designId, string strData)
{
SubmittedForm sf = new SubmittedForm();
string schema = requestSchema;
customer_DbConnection db = new customer_DbConnection();
RenderFormController renderController = new RenderFormController();
renderController.GeneratePdf(strData, db, sf);
//return RedirectToAction(model.DesignId, "Prdocut/Edit");
}
.......
And in your cshtml page simply call this path as below
<form enctype="multipart/form-data" method="post" action="~/api/ImportExport/GeneratePDF" id="frmGenerate" novalidate="novalidate" class="form-horizontal">
.....
Upvotes: 0
Reputation: 200
If you want do to some work based off a button/link, why not just use an ajax call?
For example:
<div class="col-lg-6 col-md-8 col-sm-10 ">
<button onclick=GeneratePdf('@Model.DesignId', <string data>) class="btn btn-primary enabled"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF </button>
</div>
And in your .cshtml, I would recommend having a Script section at the bottom of the file:
@section Scripts{
<script type="text/javascript">
//modify as needed to make it pass in what you need.
function GeneratePdf(designId, stringData) {
$.ajax({
url: "@Url.Action("GeneratePDFFFromHtml","Product")",
data: { designId: designId, strData: stringData },
cache: false,
contentType: false,
processData: false,
type: "POST",
success: function (data) {
//TODO: Add whatever if you want to pass a notification back
},
error: function(error) {
//TODO: Add some code here for error handling or notifications
}
}
</script>
}
Then, in your controller, you can have your function return void. NOTE: I am not really sure if you even need to pass in the DesignId but you have it in there so I will keep it there. You will most-likely need to edit this method some more to make it work properly but hopefully this will get you going.
[HttpPost]
public void GeneratePDFFFromHtml(string designId, string strData)
{
SubmittedForm sf = new SubmittedForm();
string schema = requestSchema;
customer_DbConnection db = new customer_DbConnection();
RenderFormController renderController = new RenderFormController();
renderController.GeneratePdf(strData, db, sf);
//return RedirectToAction(model.DesignId, "Prdocut/Edit");
}
Also, this will be async so you may want some notification to the user that some action is being done like a spinner.
Upvotes: 1
Reputation: 50
Try with System.Web.Mvc.EmptyResult or redirecting to same page with System.Web.Mvc.RedirectResult.
Upvotes: 0