0

MVC5 razor

The link below results in a new view in a new browser tab to allow selection of a data item, while keeping the first tab open.

@Html.ActionLink("Select a data item", "ItemDisplay", "<controller>", New With {.ID = Model.ID}, htmlAttributes:=New With {.target = "_blank"})

Once the desired data item is clicked on, can I close the new tab on the way back to the application? How do I do that?

tereško
  • 58,060
  • 25
  • 98
  • 150
Alan
  • 1,587
  • 3
  • 23
  • 43

2 Answers2

2

You can use a javascript function in the child page which can update with the selected value to the parent page and then close itself. The javascript function in the child page can be something like this:

// update the value in parent-field
window.opener.document.getElementById("parent-field").value="<some-value>"; 

// close the child window itself
window.close();

Check out this link to know how to assign a javascript function in asp.net mvc.

Note: As pointed out by Rustin, opening an entirely new popup window to achieve this will not be a good design.

Community
  • 1
  • 1
Bharat Gupta
  • 2,628
  • 1
  • 19
  • 27
  • I need to clarify for myself: your code is for a **child page**, but you note a **new popup window** is not good design. Please clarify, these are 2 different techniques in your answer, the child page & popup are different ideas? I'm actually hoping to use a new tab which I thought is a child page and not bad design. Do I understand you correctly? Considering your answer above I'm going to post a different question that's more specific but is a new thread so not appropriate here. I'll comment below with the link when it's ready & maybe you might look at it. – Alan Feb 18 '16 at 14:48
  • 1
    What I meant was you should not open another tab or a pop-up window to implement this. However, since you have already started with, hence I provided you the solution. You can rather open a modal pop-up using simple div tags and javascript. Find [this link](http://www.w3schools.com/bootstrap/bootstrap_modal.asp) to get you started with. – Bharat Gupta Feb 18 '16 at 15:03
0

Couldn't comment, so posting as answer: It is not possible, even if we can make it possible, it would be a hassle, certainly not advisable, instead you could try to open it as a partial view as a pop up or even redirect to a page and come back.

Rustin Cohle
  • 161
  • 1
  • 1
  • 12