I have been attempting to save the Root URL in a JavaScript variable within my _Layout.cshtml
like this:
<script type="text/javascript>
var rootpath = "";
$(document).ready(function () {
rootpath = "@VirtualPathUtility.ToAbsolute("~/")";
});
</script>
Then in my views:
<script src="~/Scripts/Sales/AddInvoice.js"></script>
@using(Html.BeginForm("Save","Invoice",FormMethod.Post,new { @id = "frmInvoice" }))
{
}
In my JS file, I am using rootpath
as follows:
prepUIAutocomp(rootpath + "Sales/Invoice/GetCustomer", "clientid", "ClientMst_clientname", {}, null, 1);
Everything works well until there is a postback or refresh, after which rootpath
becomes undefined
.
Since each view utilizes _Layout.cshtml
, shouldn't the path be updated with each request? Or am I overlooking something?
I would like to utilize the rootpath
variable in all my views and their associated JavaScript files for generating AJAX URLs. Any recommendations?
Thank you.