浏览器没有切换到显式调用的视图

时间:2015-10-05 19:32:02

标签: c# asp.net asp.net-mvc asp.net-mvc-5

我有一个相当奇怪的问题,我的索引中有一个按钮链接到一些Javascript然后调用我的IndicatorController StepTwo ActionResult,它在调试器中返回View行之后执行以下操作:

  1. 点击_​​ViewStart

  2. 点击StepTwo的ViewBag.Title行

  3. 然后快速浏览_Layout

  4. 命中控制器中的Dispose功能

  5. 然而,浏览器本身没有被定向到Indicator / StepTwo,我怀疑它可能是因为StepTwo视图的其余部分没有被击中,但我不确定为什么会这样。

    StepTwo ActionMethod:

    public ActionResult StepTwo(int wwStart, int wwEnd)
    {
        if (!wwStart.Equals(null))
        {
            ViewBag.wwStartSelected = wwStart;
        }
    
        if (!wwEnd.Equals(null))
        {
            ViewBag.wwEndSelected = wwEnd;
        }
        return View("StepTwo");
    }
    

    StepTwo查看:

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    @{
        ViewBag.Title = "Create Indicator: Step 2 of 3";
    }
    
    <h2>Create Indicator: Step 2 of 3 - Select Work Week Range</h2>
    
    <h3>Select Indicator Type</h3>
    
    <button type="button" class="btn btn-primary">Manager Indicator</button>
    <button type="button" class="btn btn-primary">Owner Indicator</button>
    

    指数:

    @model IEnumerable<YourProject.ViewModels.DropDownVM>
    
    
    
    @{
        ViewBag.Title = "Create Indicator: Step One of Two";
    }
    
    <h2>Indicators</h2>
    
    
    <h2>Create Indicator: Step One of Two - Select Work Week Range</h2>
    
    <h3>Select Starting Work Week</h3>
    
    @Html.DropDownList("WWStart", (SelectList)ViewBag.DDLWWStart.Categories, " -- Select Starting Work Week -- ")
    <br /><br />
    
    <h3>Select Ending Work Week</h3>
    
    @Html.DropDownList("WWEnd", (SelectList)ViewBag.DDLWWEnd.Categories, " -- Select Ending Work Week -- ")
    <br /><br />
    
    @Html.AntiForgeryToken()
    <button class="btn btn-default" id="checkbtn" onclick="nextStep();" type="button">Next Step</button>
    
    
    
    <script type="text/javascript">
        function nextStep() {
    
            var wwStartElement = document.getElementById("WWStart");
            var wwStartValue = parseInt(wwStartElement.value);
    
            var wwEndElement = document.getElementById("WWEnd");
            var wwEndValue = parseInt(wwEndElement.value);
    
            if (wwStartValue > wwEndValue) {
                alert("You selected a starting Work Week that starts before the ending Work Week. Please select a starting Work Week that is equal to or happens before the ending Work Week.");
                return false;
            }
    
            alert("Right before issue?");
            //Submit data to IndictatorController/StepTwo ActionMethod
            $.ajax({
                url: '@Url.Action("StepTwo")',
                type: "POST",
                data: {
                    'wwStart': wwStartValue,
                    'wwEnd': wwEndValue
                },
                success: function (result) {
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError);
                }
            });
        }
    </script>
    

1 个答案:

答案 0 :(得分:1)

我认为这里存在一个根本性的误解。如果您要提交AJAX请求,则不会让浏览器执行任何。来自AJAX请求的服务器响应将简单地传递给成功回调,然后由来决定它发生了什么(在DOM中插入内容等)。但是,为此,您不希望返回完整视图,包括布局和所有内容。那你知道吗?相反,您需要像部分视图这样的东西,这样您就可以在DOM中的某处插入HTML。

如果您只想将新网页加载到浏览器标签/窗口中,那么JavaScript甚至不应该发挥作用。只需做一个标准的表格帖子或将按钮作为链接。