C#HTML.Actionlink服务器上的异常行为

时间:2019-02-25 15:09:35

标签: c# html.actionlink

如果有人可以提供帮助,我将非常感激。

我在VS 2017中将C#用于ASP.net应用程序。

在剃须刀视图中,我有以下代码:

    <div class="text-center"  >

    <ul class="pagination">
        @for (var i = 1; i <= ViewBag.SearchMetaData.SearchNumberOfPages; i++)
        {
            if (ViewBag.SearchMetaData.SearchCurrentPage == @i)
            {
                <li class="active"> @Html.ActionLink(@i.ToString(), "CI_SequenceBlocks_Pager_Form_Handler", new { id = @i, value = ViewBag.SearchMetaData.SearchString, inInstitutionProgramID = ViewData["SelectedInstitutionProgram"].ToString() },null)</li>
            }
            else
            {
                <li> @Html.ActionLink(@i.ToString(), "CI_SequenceBlocks_Pager_Form_Handler", "Home", new { id = @i, value = ViewBag.SearchMetaData.SearchString, inInstitutionProgramID = ViewData["SelectedInstitutionProgram"].ToString() },null)</li>

            }
        }

    </ul>
</div>

}

在我的开发机器上效果很好。操作链接永远不会失败。

但是,当我发布到服务器时,我可以单击链接3或4次,它们可以工作。第4次之后,就像忘记了变量一样。当我查看页面源代码时,链接似乎更正。在我看来,服务器上的某些东西正在重置。有人有什么想法吗?

更多代码如下: ____________在此行之后进行编辑__________________ 控制器:

 public ActionResult CI_SequenceBlocks()
    {
        string strIP = "";
        string strSRCHString = "";
        string strSRCHstringQry = "";
        int intSRCHRequestedPage = 1;
        int intSRCHPageSize = 10;
        if (TempData["CI_SequenceBlocks_InstitutionProgram"] == null)
        {
            strIP = "0";
            strSRCHString = "";
        }
        else
        {
            strIP = TempData["CI_SequenceBlocks_InstitutionProgram"].ToString();
            strSRCHString = TempData["CI_SequenceBlocks_SRCHString"]?.ToString() ?? "";
        };
        ViewData["listInstitutionPrograms"] = new SelectList(MSSQL_CI_Repository.ListInstitutionProgramsForComboBoxes(), "Value", "Text");
        ViewData["SelectedInstitutionProgram"] = strIP;
        ViewData["inSRCHString"] = strSRCHString;
        ViewBag.SRCHString = strSRCHString;
        //ViewBag.InstitutionalProgram = strIP;
        if (strSRCHString == "") { ViewBag.SRCHDescriptor = "NO_SEARCH_TERM"; } else { ViewBag.SRCHDescriptor = strSRCHString?.ToString() ?? "NO_SEARCH_TERM"; }
        if (strSRCHString.Trim() == "")

        {
            strSRCHstringQry = "rttyghujmgdddh";
        }
        else
        {
            strSRCHstringQry = strSRCHString;
        }
        if(TempData["CI_SequenceBlocks_RequestedPage"] != null)
        {
            intSRCHRequestedPage = Convert.ToInt32(TempData["CI_SequenceBlocks_RequestedPage"]);
        }
        vm_CI_SequenceBlocksWithSearchMetaData obj = MSSQL_CI_Repository.ListCI_SequenceBlocksByInstitutionalProgramForFullTextSearch(Convert.ToInt32(strIP), strSRCHstringQry,intSRCHPageSize,intSRCHRequestedPage);
        ViewBag.SearchMetaData = obj.SearchMetaData;
        var model = obj.lstSequenceBlocks;
        return View(model);
    }

    [HttpPost]

    public ActionResult CI_SequenceBlocks_FullTextSearch(FormCollection form)
    {
        TempData["CI_SequenceBlocks_InstitutionProgram"] = form["HidInstitutionProgram"].ToString();
        TempData["CI_SequenceBlocks_SRCHString"] = form["SRCHString"].ToString();
        TempData["CI_SequenceBlocks_RequestedPage"] = 1;
        return RedirectToActionPermanent("CI_SequenceBlocks");
    }

    public ActionResult CI_SequenceBlocks_Edit_Form_Handler(int id, string value, int inInstitutionProgramID)
    {
        TempData["CI_SequenceBlocks_InstitutionProgramID"] = inInstitutionProgramID;
        TempData["CI_SequenceBlocks_SequenceBlockID"] = id;
        if (value == "Edit_SequenceBlock_ID")
        {
            return RedirectToAction("CI_SequenceBlock_Edit");
        }
        else if (value == "Maintain_SequenceBlock_ID")
        {
            return RedirectToAction("CI_SequenceBlock_Maintain");
        }
        else
        {
            TempData["CI_SequenceBlocks_InstitutionProgram"] = inInstitutionProgramID;
            return RedirectToAction("CI_SequenceBlocks");

        }

        }

    public ActionResult CI_SequenceBlocks_Pager_Form_Handler(int id, string value, int inInstitutionProgramID)
    {
        TempData["CI_SequenceBlocks_InstitutionProgram"] = inInstitutionProgramID;
        TempData["CI_SequenceBlocks_SRCHString"] = value;
        TempData["CI_SequenceBlocks_RequestedPage"] = id;
        return RedirectToActionPermanent("CI_SequenceBlocks");
    }

    [HttpPost]

    public ActionResult CI_SequenceBlocks_ProgramChange(FormCollection form)
    {
        TempData["CI_SequenceBlocks_InstitutionProgram"] = form["SelectedInstitutionProgram"].ToString();
        return RedirectToActionPermanent("CI_SequenceBlocks");
    }

RouteConfig.cs

  public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{id2}/{id3}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, id2 = UrlParameter.Optional, id3 = UrlParameter.Optional }
            );
}

全剃刀代码:

@model IEnumerable<UTCI_Manager.Models.vm_CI_SequenceBlock>

@{
    ViewBag.Title = "CI_SequenceBlocks";
}

<h2>CI_SequenceBlocks</h2>



@using (Html.BeginForm("CI_SequenceBlocks_ProgramChange", "Home", FormMethod.Post, new { id = "submitForm" }))
{

    @Html.DisplayNameFor(model => model.InstitutionProgramID)
    @Html.DropDownList("SelectedInstitutionProgram", (SelectList)ViewData["listInstitutionPrograms"], new { onchange = "this.form.submit();" })

}
@using (Html.BeginForm("CI_Sequenceblocks_FullTextSearch", "Home", FormMethod.Post, new { id = "submitForm" }))
{
    if (ViewData["SelectedInstitutionProgram"].ToString() != "0")
    {
        <div class="input-group">
            @Html.Hidden("HidInstitutionProgram", ViewData["SelectedInstitutionProgram"]?.ToString() ?? "0")
            @Html.TextBox("SRCHString", ViewData["inSRCHString"].ToString(), new { @class = "form-control", @placeholder = "Search" })
            <span class="input-group-btn">
                <button class="btn btn-info" type='submit' value="Search">Search</button>
            </span>
        </div>
    }
}
        <table class="table table-striped" summary="List of Sequence Blocks">

            <tr>

                @*     <th>
                @Html.DisplayNameFor(model => model.SequenceBlockID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.InstitutionProgramID)
            </th>
                *@
                <th>
                    @Html.DisplayNameFor(model => model.Title)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SequenceBlockDescription)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SequenceBlockRequired)
                </th>
                @*     <th>
                @Html.DisplayNameFor(model => model.Minimum)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Maximum)
            </th>
                *@

                <th>
                    @Html.DisplayNameFor(model => model.SequenceBlockTimingMethod)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Duration)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.StartDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.EndDate)
                </th>

                <th>
                    @Html.DisplayNameFor(model => model.ClerkshipModel)
                </th>

                <th>
                    @using (Html.BeginForm("CI_SequenceBlocks_AddSequenceBlock", "Home", FormMethod.Post, new { id = "submitForm" }))
                    {
                        if (ViewData["SelectedInstitutionProgram"].ToString() != "0")
                        {
                            @Html.Hidden("HidInstitutionalProgram", ViewData["SelectedInstitutionProgram"].ToString())
                            <input type="submit" value="Add Sequence Block" class="btn btn-sm" />
                        }
                    }

                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    @Html.HiddenFor(modelItem => item.SequenceBlockID)

                    @Html.HiddenFor(modelItem => item.InstitutionProgramID)

                    @Html.HiddenFor(modelItem => item.Minimum)

                    @Html.HiddenFor(modelItem => item.Maximum)


                    <td>
                        @Html.DisplayFor(modelItem => item.Title)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.SequenceBlockDescription)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.SequenceBlockRequired)
                    </td>



                    <td>
                        @Html.DisplayFor(modelItem => item.SequenceBlockTimingMethod)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Duration)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.StartDate)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.EndDate)
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.ClerkshipModel)
                    </td>

                    <td>
                        @Html.ActionLink("Edit", "CI_SequenceBlocks_Edit_Form_Handler", new { id = item.SequenceBlockID, value = "Edit_SequenceBlock_ID", inInstitutionProgramID = item.InstitutionProgramID }) |
                        @Html.ActionLink("Maintain", "CI_SequenceBlocks_Edit_Form_Handler", new { id = item.SequenceBlockID, value = "Maintain_SequenceBlock_ID", inInstitutionProgramID = item.InstitutionProgramID })


                    </td>
                </tr>
            }
        </table>
    @if (ViewData["SelectedInstitutionProgram"].ToString() != "0")
    {

        <div class="text-center"  >

            <ul class="pagination">
                @for (var i = 1; i <= ViewBag.SearchMetaData.SearchNumberOfPages; i++)
                {
                    if (ViewBag.SearchMetaData.SearchCurrentPage == @i)
                    {
                        <li class="active"> @Html.ActionLink(@i.ToString(), "CI_SequenceBlocks_Pager_Form_Handler", new { id = @i, value = ViewBag.SearchMetaData.SearchString, inInstitutionProgramID = ViewData["SelectedInstitutionProgram"].ToString() },null)</li>
                    }
                    else
                    {
                        <li> @Html.ActionLink(@i.ToString(), "CI_SequenceBlocks_Pager_Form_Handler", "Home", new { id = @i, value = ViewBag.SearchMetaData.SearchString, inInstitutionProgramID = ViewData["SelectedInstitutionProgram"].ToString() },null)</li>

                    }
                }

            </ul>
        </div>
    }


@using (Html.BeginForm("CI_SequenceBlocks_AddSequenceBlock", "Home", FormMethod.Post, new { id = "submitForm" }))
{
    if (ViewData["SelectedInstitutionProgram"].ToString() != "0")
    {
        <table class="table">
            <tr>
                <td>
                    @Html.Hidden("HidInstitutionProgram", ViewData["SelectedInstitutionProgram"].ToString())
                </td>
            </tr>
            <tr>
                <td>Add Sequence Block:</td>
                <td> <input type="submit" value="Add Sequence Block" class="btn btn-sm" /> </td>
            </tr>
        </table>
    }
}

____________最新更新____________

发现了问题:

我将RedirectToActionPermanent更改为RedirectToAction。没问题了!

谢谢

詹姆斯

0 个答案:

没有答案