如何在单击按钮时不打开新窗口

问题描述 投票:0回答:1

我正在开发一个 ASP.NET Core MVC 项目。我目前的索引视图页面和主控制器正在工作。我遇到的唯一问题是每当我单击“搜索”按钮时,结果都会显示在新选项卡中。我尝试过改变

<button type="submit" id="searchButton> Search </button> 

<input type="submit" value="Search"/> 

但这没有用。我想弄清楚是否有办法使结果出现在同一个窗口上。如有任何帮助,我们将不胜感激。

查看:

@model IEnumerable<Officer>
@{
    ViewData["Title"] = "FLIP";
    Layout = "_Layout";
}

<meta name="vs_defaultClientScript" content="JavaScript" />
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5" />
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0" />
<meta name="ProgId" content="VisualStudio.HTML" />
<meta name="Originator" content="Microsoft Visual Studio.NET 7.0" />
<base target="mainpage" />
<!--Header-->

<!--Body of the page-->
<html>
<head>
    <title>@ViewData["Title"]</title>
</head>
<hr style="width:75%" />
<body>

    <form id="SearchForm"  asp-controller="Home" asp-action="Search">
        <div class="full-width">
            <table border="0">
                <tr>
                    <td vAlign="top" width="400" bgColor="aliceblue">
                        <h6>
                            <Font face="Arial" size="2">
                                &nbsp;DIRECTIONS: To find an interpreter . . .
                            </Font>
                            <!-- controls for specifying the required Agency ID or name --><br>
                        </h6>
                        <p>
                            
                                @{
                                    Html.RenderPartial("~/Views/Shared/LanguageList.cshtml");
                                }
                            
                            <br />
                            <Font face="Arial" size="2">
                                2) Enter one of the following, if you want to restrict your search:
                            </Font>
                            <table>
                                <tr>
                                    <td align="right">City:&nbsp;</td>
                                    <td>
                                        <input type="text" id="city" name="city" maxlength="100" colmuns="20" width="200">
                                    </td>
                                        
                                </tr>
                                <tr>
                                    <td align="right">State:&nbsp;</td>
                                    <td>
                                        <select name="State" id="State">
                                            <option Value=""> Select a State . . .</option>
                                            <option value="DE">Delaware</option>
                                            <option value="DC">District of Columbia</option>
                                            <option value="IN">Indiana</option>
                                            <option value="MD">Maryland</option>
                                            <option value="MI">Michigan</option>
                                            <option value="NJ">New Jersey</option>
                                            <option value="NY">New York</option>
                                            <option value="OH">Ohio</option>
                                            <option value="PA">Pennsylvania</option>
                                            <option value="VA">Virginia</option>
                                            <option value="ON">Ontario</option>
                                            <option value="QC">Quebec</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">Zip:&nbsp;</td>
                                    <td>
                                        <input type="text" id="zip" name="zip" maxlength="5" colmuns="20" width="100">
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <br />
                                        <button type="submit" id="searchButton" >Search</button>
                                    </td>
                                </tr>
                            </table>
                            <br />
                            <p>@ViewBag.errorMessage</p>
                            <td>
                                <FONT face="Arial" size="2">
                                    <p>This program was designed by MAGLOCLEN to assist investigators 
                                    dealing with issues of foreign languages<br>
                                    by providing access to information on law enforcement officers with language
                                <br>
                                    skills.
                                    </p>
                                    <p>To find an interpreter:</p>
                                    <ol>
                                        <li>Select a language from the picklist on the left.</li>
                                        <li>[Optional] Limit your search by an area surrounding your zip code. This will<br>
                                        narrow the list of speakers for the language you selected to those geographically<br>
                                        closest to the zip code you enter.
                                        </li>
                                    </ol>                              
                            
                                    <p>To enter information on yourself or someone in your agency who speaks a language:</p>
                                    <ol>
                                        <li>Click on "Administer Agency" and search for your agency.</li>
                                        <li>
                                            Follow the instructions provided to enter a speaker, language, and contact information
                                        </li>
                                    </ol>  
                                    <p>Thank you for participating in this program. MAGLOCLEN hopes it is useful to you and your agency. 
                                        Please feel free to use the "Contact" tab at the top of this page to provide feedback.
                                    </p>
                                </font>
                            </td>
                        </p>
                    </td>
                </tr>
            </table>

        </div>
        
    </form>

    @if (Model != null && Model.Any())
    {
        <br />
        <h6>Click details link in the grid below to list detail information on how to contact the officer.</h6>
        <!--<h6 style="margin-left:-250px;">Click a Column Heading to sort the grid</h6>-->
        <table class="table table-bordered table-striped" style="width:75%;
        background-color: white; border:1px solid black;">
            <thead>
                <tr>
                    <th>Officer</th>
                    <th>Fluency</th>
                    <th>Phone</th>
                    <th>Extension</th>
                    <th>Cell Phone</th>
                    <th>City</th>
                    <th>State</th>
                    <th>Zip Code</th>
                    <th>Agency</th>
                    <th>Address</th>
                    <th>Date Modified</th>

                </tr>
            </thead>
            @foreach (var officer in Model)
            {
                <tr>
                    <td>
                        @officer.FirstName
                        @officer.LastName
                    </td>
                    <td>@officer.Fluency</td>
                    <td>@officer.Phone</td>
                    <td>@officer.Ext</td>
                    <td>@officer.Cell</td>
                    <td>@officer.City</td>
                    <td>@officer.State</td>
                    <td>@officer.PostalCode</td>
                    <td>@officer.AgencyName</td>
                    <td>@officer.Address1</td>
                    <td>@officer.DateUpdated</td>
                    <td>
                        <a asp-action="Details" asp-route-id="@officer.OfficerKey">Details</a>
                    </td>
                </tr>
            }
        </table>
    }


    <script src=https://code.jquery.com/jquery-3.6.0.min.js></script>
    <script>
        $('#State').click(function () {
            $('#searchForm').button();
        });
    </script>


    <style>
 
        .body-container {
            /*remove the container default padding attribute*/
            /*padding-left: 0px !important;
            padding-right: 0px !important;*/
            /*increase width as per your need*/
            max-width: 100%;
        } 
    </style>

</body>
</html>

控制器:

 public IActionResult Index()
 {
     return View();
 }

 //Get Officer from search 
 [HttpPost]
 public IActionResult Search( SearchQueryModel searchQuery )
 {
        MailkitErrors errors = new(ServiceProvider!, Configuration!);

        UserEmail = HttpContext.Session.GetString("UserEmail")!;

        string ConnectionString = connectionStrings.Get( "ForeignLanguageConnectString" );

     var objConnect = new SqlConnection( ConnectionString );
     var objDataSet = new DataSet();
     string strQuery = "SearchLanguageByMany";

     try
     {
         //NO Language is selected 
         if (searchQuery == null || searchQuery.LanguageName == null)
         {
             ViewBag.errorMessage = "Please select a language from the drop-down menu, then click search";
             return View("Index");
         }

         //Language is selected 
         var objCommand = new SqlCommand( strQuery, objConnect );
         objCommand.CommandType = CommandType.StoredProcedure;
         objCommand.CommandTimeout = 360;
         objCommand.Parameters.AddWithValue( "@State", searchQuery.State );
         objCommand.Parameters.AddWithValue( "@LanguageName", searchQuery.LanguageName );
         objCommand.Parameters.AddWithValue( "@City", searchQuery.City );
         objCommand.Parameters.AddWithValue( "@Zip", searchQuery.Zip );

         var objDataAdapter = new SqlDataAdapter( objCommand );
         objDataAdapter.Fill( objDataSet, "Officer" ); //FLIPV2 dbo.Officer table
     }
     catch (Exception)
     {
            ViewBag.ErrorMessage = "There has been an error. MAGLOCLEN HelpDesk has been notified.";
            errors.ErrorEmails(UserEmail, "Error retrieving officers:\n");
        }

     List<Officer> Officers = new List<Officer>();

     foreach (DataRow row in objDataSet.Tables[ "Officer" ]!.Rows)
     {
         Officer ofc = new Officer();

         if (row[ "OfficerKey" ] is DBNull) ofc.OfficerKey = 0;
         else ofc.OfficerKey = (int)row[ "OfficerKey" ];

         if (row[ "AgGuid" ] is DBNull) ofc.AgGuid = Guid.Empty;
         else ofc.AgGuid = (Guid)row[ "AgGuid" ];

         if (row[ "AgencyName" ] is DBNull) ofc.AgencyName = "";
         else ofc.AgencyName = (string)row[ "AgencyName" ];

         if (row[ "OfficerRank" ] is DBNull) ofc.OfficerRank = "";
         else ofc.OfficerRank = (string)row[ "OfficerRank" ];

         if (row[ "FirstName" ] is DBNull) ofc.FirstName = "";
         else ofc.FirstName = (string)row[ "FirstName" ];

         if (row[ "LastName" ] is DBNull) ofc.LastName = "";
         else ofc.LastName = (string)row[ "LastName" ];

         if (row[ "Fluency" ] is DBNull) ofc.Fluency = "";
         else ofc.Fluency = (string)row[ "Fluency" ];

         if (row[ "Phone" ] is DBNull) ofc.Phone = "";
         else ofc.Phone = (string)row[ "Phone" ];

         if (row[ "Ext" ] is DBNull) ofc.Ext = "";
         else ofc.Ext = (string)row[ "Ext" ];

         if (row[ "Cell" ] is DBNull) ofc.Cell = "";
         else ofc.Cell = (string)row[ "Cell" ];

         if (row[ "Address1" ] is DBNull) ofc.Address1 = "";
         else ofc.Address1 = (string)row[ "Address1" ];

         if (row[ "City" ] is DBNull) ofc.City = "";
         else ofc.City = (string)row[ "City" ];

         if (row[ "State" ] is DBNull) ofc.State = "";
         else ofc.State = (string)row[ "State" ];

         if (row[ "Country" ] is DBNull) ofc.Country = "";
         else ofc.Country = (string)row[ "Country" ];

         if (row[ "PostalCode" ] is DBNull) ofc.PostalCode = "";
         else ofc.PostalCode = (string)row[ "PostalCode" ];

         if (row[ "Email" ] is DBNull) ofc.Email = "";
         else ofc.Email = (string)row[ "Email" ];

         if (row[ "DateUpdated" ] is DBNull) ofc.DateUpdated = System.DateTime.MinValue;
         else ofc.DateUpdated = (System.DateTime)row[ "DateUpdated" ];

         if (row[ "UserUpdated" ] is DBNull) ofc.UserUpdated = "";
         else ofc.UserUpdated = (string)row[ "UserUpdated" ];

         if (row[ "Address2" ] is DBNull) ofc.Address2 = "";
         else ofc.Address2 = (string)row[ "Address2" ];

         Officers.Add( ofc );
     }

     return View( "Index", Officers );
}
asp.net-mvc asp.net-core
1个回答
0
投票

您应该删除/注释此行。

<base target="mainpage" />
© www.soinside.com 2019 - 2024. All rights reserved.