-1

I had a Details view which it's contain a Save and AddLine button. The AddLines button will call javascript function "AddLinePopup" to open a new window ("window.open('./AddLine?id=' + id") with Add Line view. This was working correctly when open the Details view directly. The problem occurred when the Save button is clicked and it call to Controller (PRController) to save data and redirect back to Details view again. The URL in AddLinePopup javascript function will append the controller ("'PR/Details/AddLine?id=1") when AddLine button is clicked and causing the URL error. I did tried using "window.open('~/PR/AddLine?id=' + id')", "window.open('../PR/AddLine?id=' + id')" not working as well. How can I render the javascript AddLinePopup function URL correctly ("'PR/AddLine?id=1") after Save button is clicked? Any suggestion or solution is highly appreciated. Thanks in advance.

//PRController
public ActionResult Save(DetailsModel pr)
{
     return RedirectToAction("Details", new { id = pr.ID });
}


//View
@using (Html.BeginForm("Save", "PR", FormMethod.Post))
{
    <input type="submit" value="Save" class="btn btn-primary" title="Save changes" />
}

@Html.ActionLink("Line", "AddLine", null, new { @class = "fa fa-plus-square btn btn-primary", 
title = "Add new line", onclick = "AddLinePopup('" + @Model.ID + "');return false;" })


<script type="text/javascript">
     AddLinePopup = function (id) {window.open('./AddLine?id=' + id, "AddLineWindow", target = '_blank','width=800px,height=650px,top=10,left=250');
}
</script>
2
  • 1
    You can use the @Url.Action(....) method to construct urls. However this doesn't work very well if you need to substitute parameter values in javascript. See stackoverflow.com/questions/15112055/… Commented Mar 28, 2022 at 1:12
  • This solve my problem. I use <a href="javascript:AddLinePopup('@Url.Action("AddLine", "PR")','@Model..ID')"> and open new window in javascript. AddLinePopup = function (url,id) { window.open(url + '?id=' +id,"addlineWindow",'width=800px,height=400px,top=10,left=e250');} Commented Mar 28, 2022 at 6:06

1 Answer 1

0

This solve my problem. I use link to call Javascript function and open new window in JavaScript.

`<a href="javascript:AddLinePopup('@Url.Action("AddLine", "PR")','@Model..ID')">

AddLinePopup = function (url,id) { window.open(url + '?id=' +id,"addlineWindow",'width=800px,height=400px,top=10,left=e250');}`

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.