A form can only be submitted using the submit button to the URL indicated by its action attribute.
However, you can submit the form data to a different URL using the jQuery post method, manually checking the form before submitting it. Thus, you can send form data to the controller method PreviewWordDocument and process the response to show the preview in the desired div. (It will be useful if you provide a form identifier so that you can easily find it using jQuery)
So, your click event handler for the preview link will look like this:
$("#previewLink").click(function(e) { e.preventDefault(); if($("#YourFormId").valid()){ $("#dialogcontainer").dialog('open'); } });
In the open dialog box function, you will display the form (which has already been verified) on the preview controller method using the jQuery ajax function. The response will be loaded into the div dialogContent div:
$.ajax({ type: "POST", url: $("#previewLink").attr("href"), //the preview controller method data: $("#YourFormId").serialize(), success: function (data) { //load ajax response into the dialogContent div $("#dialogcontent").html(data); }, error: function(xhr, error) { $("#YourFormId").prepend('<div id="ajaxErrors"></div>') .html(xhr.responseText); } });
Now you can get the whole document in the PreviewWordDocument action:
public ActionResult PreviewWordDocument(WordAutomation.Models.Document model) { var image = Url.Content("~/Content/preview.jpeg"); return PartialView((object)image); }
source share