Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open cshtml file in new tab from controller's method?

I'm working on a Nopcommerce, and need to generate Invoice (custom made not as what they already provide, because it just doesn't solve our purpose). We need to generate Invoice in new tab(using another cshtml file) using Controller's method also I'm passing model data on view.

<tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.ProbableDeliveryDate):
        </td>
        <td class="adminData">
            @Html.EditorFor(model=>model.ProbableDeliveryDate)
        </td>
        </tr>
        <tr>
            <td>
            @if(Model.CanGenrateInvoice)
            {
                 <input type="submit" name="generateinvoice" value="@T("Admin.Orders.Fields.generateinvoice")" id="generateinvoice" class="adminButton"  />
            }
            </td>
        </tr>

I've to post data to get value of probableDeliveryDate to controller method and after that want to open view in new tab.

How can i do this?

like image 986
Dharmik Bhandari Avatar asked May 12 '12 12:05

Dharmik Bhandari


People also ask

How do I read a Cshtml file?

Right click the Index. cshtml file and select View in Browser. You can also right click the Index. cshtml file and select View in Page Inspector.

What is Cshtml file format?

A CSHTML file is a C# HTML webpage file used by Razor, an ASP.NET view engine that generates webpages. It is similar to a standard ASP.NET webpage (. ASP or . ASPX file) but uses slightly different syntax. CSHTML files run on a web server, which generates HTML for a client web browser.


1 Answers

If you are getting to the action from the first page via an Html.ActionLink you can do this:

Html.ActionLink("Open Invoice", "ActionName","ControllerName", new { id = Model.InvoiceID }, new { target = "_blank" });

Specifying target = "_blank" will open in the new tab

Update

Since you are posting the model to the controller (I was hoping RedirectToAction could help open a new window/tab but that doesn't look to be the case)

My spidy sense is tingling on the flow you have tho... This is just me but I would do something a little different.. such as

  1. Post the model to the controller
  2. Save the data that generates the invoice
  3. Return the InvoiceID to the action
  4. Add the InvoiceID to the model
  5. Send the model back to the view
  6. Inform the user that
  7. the invoice was generated and show a link - like above - that allows the user to open the invoice OR
  8. this provides the perfect clean solution to show model errors if there were any

Your view could have a piece of razor code that did that:

@{
    if(Model.InvoiceID != null && Model.InvoiceID !=0) {
        @Html.ActionLink("Open Invoice", "ActionName","ControllerName", new { id = Model.InvoiceID }, new { target = "_blank" });
    } 
}
like image 149
CD Smith Avatar answered Sep 22 '22 08:09

CD Smith