简体   繁体   English

为什么在加载视图和单击ASP.NET MVC中的Html.ActionLink之间,我的routeValues变量会发生变化?

[英]Why is my routeValues variable changing between my view loading and me clicking on my Html.ActionLink in ASP.NET MVC?

I have an ASP.NET MVC application where I have a textbox with a jquery datePicker and a HTML.ActionLink that will download an Excel document that uses the date string picked from the datePicker. 我有一个ASP.NET MVC应用程序,其中有一个带有jquery datePicker和HTML.ActionLink的文本框,该文本框将下载使用从datePicker选取的日期字符串的Excel文档。 I want the link to the Excel download to use this date string as a parameter in its query string. 我希望到Excel下载的链接使用此日期字符串作为其查询字符串中的参数。

In the example code I will provide sample names for my classes and variables. 在示例代码中,我将为我的类和变量提供示例名称。

Here is an excerpt from my main view (View is named "TestView", Model is of class "TestModel"): 这是我的主视图的摘录(视图名为“ TestView”,模型属于“ TestModel”类):

        @using (Ajax.BeginForm("TestForm", FormMethod.Post,
        new AjaxOptions
        {
            UpdateTargetId = "IdSomething",
            OnFailure = "handleFailure(xhr, status, 'IdSomething')"
        }))
    {
        @Html.AntiForgeryToken()
        @Html.Action("TestSettings", Model)
        <div class="clear margin-bottom-adjusted">
            @Html.ActionLink(Markup.Download_As_Microsoft_Excel, "Download",
            new { InstantlyUpdatedDate = Model.InstantlyUpdatedDate },
            new { @class = "download-excel" });
        </div>
    }

Here is the relevant excerpt from the "TestSettings" view(it also uses a model of the class "TestModel"). 这是“ TestSettings”视图中的相关摘录(它也使用类“ TestModel”的模型)。 The submit button here and the variable "NotInstantlyUpdatedDate" are used for updating a graph, but this should not be relevant for the Excel download: 此处的“提交”按钮和变量“ NotInstantlyUpdatedDate”用于更新图形,但这与Excel下载无关:

<div>
    @Html.TextBoxFor(m => m.NotInstantlyUpdatedDate, new { @class = 
    "datepicker", @onchange = "setExcelDownloadDate(" + @Json.Encode(Model) + 
    ", $(this))" })
    <input type="submit" value="@Markup.Update" class="btn" />
</div>

The "setExcelDownloadDate" function is defined in javascript like this: 像这样在javascript中定义“ setExcelDownloadDate”函数:

function setExcelDOwnloadDate(model, item) {
    if (item.attr('name') === "NotInstantlyUpdatedDate")
    model.InstantlyUpdatedDate = item.val();

$.ajax({
    url: '../Test/UpdateTestView',
    type: 'post',
    data: {
        model: model
    }
});

} }

Relevant excerpt from my Controller: 我的控制器的相关摘录:

public TestController

{

    //Should only get called once by another view
    [HttpPost]
    public ActionResult InitTestView(TestModel model)
    {
         model.NotInstantlyUpdatedDate = "2018, 01, 01";
         model.InstantlyUpdatedDate = model.NotInstantlyUpdatedDate;
         return PartialView("TestView", model);
    }
    [HttpPost]
    public ActionResult UpdateTestView(TestModel model)
    {
        return PartialView("TestView", model);
    }

    [HttpPost]
    public Task<ActionResult> Download(DownloadModel model)
    {
       //download the model here
    }
}

Here is my "TestModel" class for this example: 这是此示例的“ TestModel”类:

[Serializable]
public class TestModel
{
    public string NotInstantlyUpdatedDate { get; set; }
    public string InstantlyUpdatedDate { get; set; }
}

And here is my DownloadModel class: 这是我的DownloadModel类:

public class DownloadModel
{
    public string InstantlyUpdatedDate { get; set; }
}

OK, thank you for bearing with me. 好,谢谢您的支持。 Here is what's happening: 这是正在发生的事情:

First the InitTestView method is called, and it renders a TestView. 首先调用InitTestView方法,然后呈现一个TestView。 If I place a breakpoint at the @Html.ActionLink line in the TestView, it will show me that the model.InstantlyUpdatedDate variable is "2018, 01, 01" (this is correct and the expected behaviour). 如果我在TestView的@ Html.ActionLink行上放置一个断点,它将告诉我model.InstantlyUpdatedDate变量是“ 2018,01,01”(这是正确的,也是预期的行为)。

Since the TestSettings view is embedded in the TestView, it will render the Html.TextBoxFor for my datepicker. 由于TestSettings视图嵌入在TestView中,它将为我的日期选择器呈现Html.TextBoxFor。 If I now inspect the Download button in my browser, a correct download query string will show, with the date "2018, 01, 01" as a parameter. 如果现在检查浏览器中的“下载”按钮,将显示正确的下载查询字符串,并将日期“ 2018、01、01”作为参数。

Now, let's say I pick the date ("2018, 01, 02") from the datepicker (the conversion to a date string is done in jquery, don't worry about this as it's working as expected). 现在,假设我从datepicker中选择了日期(“ 2018、01、02”)(转换为日期字符串是在jquery中完成的,不用担心,因为它可以按预期工作)。 This date will now show in the textbox, and the @onchange event will trigger my javascript function setExcelDownloadDate. 现在,该日期将显示在文本框中,并且@onchange事件将触发我的JavaScript函数setExcelDownloadDate。 In this method, I can put breakpoints and see that my model.InstantlyUpdatedDate has indeed been set to "2018, 01, 02". 在这种方法中,我可以放置断点并查看我的模型。InstantlyUpdatedDate实际上已经设置为“ 2018,01,02”。 This is the correct behaviour. 这是正确的行为。

From the javascript function the ajax call sends the model object to my TestController. 通过javascript函数,ajax调用将模型对象发送到我的TestController。 If I break in the function UpdateTestView, I can look at my model variable and also see here that the value has changed to "2018, 01, 02". 如果我中断了UpdateTestView函数,则可以查看我的模型变量,并且在此处还看到该值已更改为“ 2018,01,02”。 Working correctly. 工作正常。

Now that this method returns a new instance of my TestView with the updated model, I can still break at the Html.ActionLink line and see that yes indeed, the Model.InstantlyUpdatedDate is "2018, 01, 02", which is correct. 现在,此方法返回了具有更新后的模型的TestView的新实例,我仍然可以在Html.ActionLink行处中断,然后看到确实是,Model.InstantlyUpdatedDate是“ 2018,01,02”,这是正确的。

However, here comes the problem. 但是,问题来了。 If i inspect the link in my browser, I will see that the url is incorrect. 如果我在浏览器中检查链接,我会看到该URL不正确。 The date is not "2018, 01, 02", but still "2018, 01, 01". 日期不是“ 2018,01,02”,而是“ 2018,01,01”。 If I click the link and put a breakpoint in my Download method, the model's InstantlyUpdatedDate property will also be "2018, 01, 01" instead of "2018, 01, 02". 如果单击链接并在我的Download方法中放置一个断点,则模型的InstantlyUpdatedDate属性也将是“ 2018,01,01”,而不是“ 2018,01,02”。

For some reason the model property InstantlyUpdatedDate seems to change back to it's original value. 由于某种原因,模型属性InstantlyUpdatedDate似乎会变回其原始值。 I do not know why this happens, and therefore I ask if some of you may be able to help me. 我不知道为什么会这样,因此我问你们中有些人是否可以帮助我。 This is part of a larger codebase, and something I don't know about might of course screw with what's happening. 这是一个更大的代码库的一部分,我当然不知道这可能与发生的事情有关。 It could also be that this is the expected behaviour for some reason, and that I'm just not familiar enough with how this should work. 也可能是出于某种原因,这是预期的行为,而我对此的工作方式还不十分了解。

Thank you for your time. 感谢您的时间。

I have a bit of trouble following this but I'll give a try. 我在执行此操作时遇到了一些麻烦,但我会尝试。 It seems like you aren't doing anything with the result of $.ajax. 似乎您没有对$ .ajax的结果做任何事情。 It will return the partial view with everything filled up but nothing is done with it. 它将返回已填充所有内容的部分视图,但不会执行任何操作。

$.ajax({
    url: '../Test/UpdateTestView',
    type: 'post',
    data: {
        model: model
    }
}).done(function( html ) {
    // $( "#results" ).append( html ); // Put the html somewhere
  });

Personally, in the onchange, I would just update the link instead of the whole partial view. 就个人而言,在onchange中,我将只更新链接而不是整个局部视图。 I usually update the partial view when there's a bit more changes than just a link. 当变化多于链接时,我通常会更新部分视图。

$('#linkId').attr('href', '@Url.Action(Markup.Download_As_Microsoft_Excel, "Download")?InstantlyUpdatedDate=' + item.val());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM