从View返回到Controller时未设置模型值

时间:2021-11-21 11:20:40

I have a boolean model variable who's value is supposed to be set to TRUE in order to perform a process on return back into the Controller.

我有一个布尔模型变量,其值应该设置为TRUE,以便在返回到Controller时执行一个进程。

It works absolutely fine on my local machine, but not on the remote web server.

它在我的本地计算机上工作得很好,但在远程Web服务器上却没有。

Can somebody PLEASE inform me what I am missing? Below is the "proof of the pudding": The boolean value in quesion is "ShouldGeneratePdf";

有人可以告诉我我错过了什么吗?下面是“布丁的证明”:问题中的布尔值是“ShouldGeneratePdf”;

MODEL:

namespace PDFConverterModel.ViewModels
{
    public partial class ViewModelTemplate_Guarantors
    {
        public ViewModelTemplate_Guarantors()
        {
            Templates = new List<PDFTemplate>(); 
            Guarantors = new List<tGuarantor>(); 
        } 

        public int SelectedTemplateId { get; set; }
        public List<PDFTemplate> Templates { get; set; }

        public int SelectedGuarantorId { get; set; }
        public List<tGuarantor> Guarantors { get; set; }

        public string LoanId { get; set; }
        public string DepartmentId { get; set; }
        public bool isRepeat { get; set; }
        public string ddlDept { get; set; }
        public string SelectedDeptText { get; set; }
        public string LoanTypeId { get; set; }
        public string LoanType { get; set; }

        public string Error { get; set; }
        public string ErrorT { get; set; }
        public string ErrorG { get; set; }
        public bool ShowGeneratePDFBtn { get; set; }
        public bool ShouldGeneratePdf { get; set; }
    }
}

MasterPage:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/kendo/2012.2.913/kendo.common.min.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/kendo/2012.2.913/kendo.dataviz.min.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/kendo/2012.2.913/kendo.blueopal.min.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-2.5.3.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/kendo/2012.2.913/kendo.all.min.js")"></script>
    <script src="@Url.Content("~/Scripts/kendo/2012.2.913/kendo.aspnetmvc.min.js")"></script>
</head>

<body>
    <div class="page">
        <header>
            <div id="title">
                <h1>BHG :: PDF Service Generator</h1>
            </div>
        </header>
        <section id="main">
            @RenderBody()
        </section>
        <footer>
        </footer>
    </div>
</body>
</html>

View:

@model PDFConverterModel.ViewModels.ViewModelTemplate_Guarantors

@using (Html.BeginForm("ProcessForm", "Home", new AjaxOptions { HttpMethod = "POST" }))
{ 
    <table style="width: 1000px">

        @Html.HiddenFor(x => x.ShouldGeneratePdf)

        <tr>
            <td>
                <img alt="BHG Logo" src="~/Images/logo.gif" />
            </td>
        </tr>
        <tr>
            <td>
                @(Html.Kendo().IntegerTextBox()
                  .Placeholder("Enter Loan Id")
                  .Name("LoanId")
                  .Format("{0:#######}")
                  .Value(Convert.ToInt32(Model.LoanId))
                )
            </td>
        </tr>
        <tr>
            <td>@Html.Label("Loan Type: ")
                @Html.DisplayFor(model => Model.LoanType)
            </td>
            <td>
                <label for="ddlDept">Department:</label>
                @(Html.Kendo().DropDownListFor(model => Model.ddlDept)
                            .Name("ddlDept")
                            .DataTextField("DepartmentName")
                            .DataValueField("DepartmentID")
                            .Events(e => e.Change("Refresh"))
                            .DataSource(source =>
                            {
                                source.Read(read =>
                                {
                                    read.Action("GetDepartments", "Home");
                                });
                            })
                            .Value(Model.ddlDept.ToString())
                    )
            </td>
        </tr>

        @if (Model.ShowGeneratePDFBtn == true)
        {
            if (Model.ErrorT == string.Empty)
            {
            <tr>
                <td>
                    <u><b>@Html.Label("Templates:")</b></u>
                </td>
            </tr>
            <tr>
                @for (int i = 0; i < Model.Templates.Count; i++)
                {  
                    <td>
                        @Html.CheckBoxFor(model => Model.Templates[i].IsChecked)
                        @Html.DisplayFor(model => Model.Templates[i].TemplateId)
                    </td> 
                }

            </tr>
            }
            else
            {
            <tr>
                <td>
                    <b>@Html.DisplayFor(model => Model.ErrorT)</b>
                </td>
            </tr>
            }

            if (Model.ErrorG == string.Empty)
            {
            <tr>
                <td>
                    <u><b>@Html.Label("Guarantors:")</b></u>
                </td>
            </tr>
            <tr>
                @for (int i = 0; i < Model.Guarantors.Count; i++)
                { 
                    <td>
                        @Html.CheckBoxFor(model => Model.Guarantors[i].isChecked)
                        @Html.DisplayFor(model => Model.Guarantors[i].GuarantorFirstName)&nbsp;@Html.DisplayFor(model => Model.Guarantors[i].GuarantorLastName)
                    </td> 
                }

            </tr>
            }
            else
            {
            <tr>
                <td>
                    <b>@Html.DisplayFor(model => Model.ErrorG)</b>
                </td>
            </tr>
            }
        }
        <tr>
            <td>
                <input type="submit" name="submitbutton" id="btnRefresh" value='Refresh' />
            </td>
            @if (Model.ShowGeneratePDFBtn == true)
            {
                <td>
                    <input type="submit" name="submitbutton" id="btnGeneratePDF" value='Generate PDF' />
                </td>
            }
        </tr>
        <tr>
            <td style="color: red; font: bold">
                @Model.Error
            </td>
        </tr>

    </table>
}

<script type="text/javascript">

    $('#btnRefresh').click(function () {
        Refresh();
    });

    function Refresh() {

        var LoanID = $("#LoanID").val();

        if (parseInt(LoanID) != 0) {
            $('#ShouldGeneratePdf').val(false)
            document.forms[0].submit();
        }
        else {
            alert("Please enter a LoanId");
        }
    }

    //$(function () {
    //    //DOM loaded
    //    $('#btnGeneratePDF').click(function () {
    //        DisableGeneratePDF();
    //        $('#ShouldGeneratePdf').val(true)
    //    });
    //});

    //function DisableGeneratePDF() {
    //    $('#btnGeneratePDF').attr("disabled", true);
    //    $('#btnRefresh').attr("disabled", true);
    //}


    $('#btnGeneratePDF').click(function () {
        alert("inside click function");
        DisableGeneratePDF();
        $('#ShouldGeneratePdf').val(true)
        tof = $('#ShouldGeneratePdf').val();
        alert("ShouldGeneratePdf set to " + tof);
    });

    function DisableGeneratePDF() {
        alert("begin DisableGeneratePDF function");
        $('#btnGeneratePDF').attr("disabled", true);
        $('#btnRefresh').attr("disabled", true);
        alert("end DisableGeneratePDF function");
    }

</script>

Controller:

[HttpPost]
        public ActionResult ProcessForm(string submitbutton, ViewModelTemplate_Guarantors model, FormCollection collection)

if ((submitbutton == "Refresh") || (submitbutton == null) && (model.ShouldGeneratePdf == false))
            {
}
else if ((submitbutton == "Generate PDF") || (model.ShouldGeneratePdf == true))
            {
}

The "Alerts" in the script above come out to exactly what they should be on the remote server. The last alert shows that the value of the bool variable is "true". However, when I do page source views of the hidden variable, below is the result.

上面脚本中的“警报”确切地说明了它们在远程服务器上应该是什么。最后一个警报显示bool变量的值为“true”。但是,当我执行隐藏变量的页面源视图时,下面是结果。

The values of the hidden variable when the page loads and when the last alert button finishes are as follows:

页面加载时和上次警告按钮完成时隐藏变量的值如下:

My local machine:

我的本地机器:

The remote machine:

远程机器:

As you can see, the value on my machine is set to true when the process executes. However, on the remote machine, it is set to false where it then doesn't excute.

如您所见,当进程执行时,我的机器上的值设置为true。但是,在远程计算机上,它设置为false,然后不执行。

Why isn't the value in the model being returned as TRUE on the remote machine?

为什么模型中的值在远程计算机上没有返回为TRUE?

2 个解决方案

#1


0  

This might just be a typo, but the line

这可能只是一个错字,但行

$('#ShouldGeneratePdf').val(true) 

does not have the closing ;

没有结束;

$('#ShouldGeneratePdf').val(true);

#2


0  

Found out what the issue is....

发现问题是什么....

Using Fiddler, I was able to determine that the website runs just fine under FireFox (v16.0.2). I could see the value getting changed in Fiddler to true where it would execute the normal process.

使用Fiddler,我能够确定该网站在FireFox(v16.0.2)下运行得很好。我可以看到Fiddler中的值变为true,它将执行正常的进程。

For the latest version of Chrome, it did not work (v23.0.1271.84).

对于最新版本的Chrome,它无效(v23.0.1271.84)。

If using IE9, it automatically jumps into Compatability mode when going to the site. In IE9, if Compatability View is being used, it doesn't work. Once I changed from compatability View back to just IE9, it worked fine. Prior to IE9 (IE8 & IE7), did not work either.

如果使用IE9,它会在进入站点时自动跳转到Compatability模式。在IE9中,如果正在使用Compatability View,则它不起作用。一旦我从兼容性视图改回到IE9,它工作正常。在IE9(IE8和IE7)之前,也没有用。

How and when can these issues be fixed by Microsoft & Google?

微软和谷歌如何以及何时解决这些问题?

Any suggestions in the meantime to work around this?

在此期间有任何建议可以解决这个问题吗?

I unchecked the box under the Tools | compatability View Settings and unchecked the box that says "Display intranet sites in Compatability mode."

我取消选中工具|下的框兼容性查看设置并取消选中“在兼容模式下显示Intranet站点”框。

So, now when I go into IE, it automatically puts me into regular IE9 mode.

所以,现在当我进入IE时,它会自动进入常规的IE9模式。

Anyone know of a similiar setting for Chorme?

有人知道Chorme的类似设置吗?

#1


0  

This might just be a typo, but the line

这可能只是一个错字,但行

$('#ShouldGeneratePdf').val(true) 

does not have the closing ;

没有结束;

$('#ShouldGeneratePdf').val(true);

#2


0  

Found out what the issue is....

发现问题是什么....

Using Fiddler, I was able to determine that the website runs just fine under FireFox (v16.0.2). I could see the value getting changed in Fiddler to true where it would execute the normal process.

使用Fiddler,我能够确定该网站在FireFox(v16.0.2)下运行得很好。我可以看到Fiddler中的值变为true,它将执行正常的进程。

For the latest version of Chrome, it did not work (v23.0.1271.84).

对于最新版本的Chrome,它无效(v23.0.1271.84)。

If using IE9, it automatically jumps into Compatability mode when going to the site. In IE9, if Compatability View is being used, it doesn't work. Once I changed from compatability View back to just IE9, it worked fine. Prior to IE9 (IE8 & IE7), did not work either.

如果使用IE9,它会在进入站点时自动跳转到Compatability模式。在IE9中,如果正在使用Compatability View,则它不起作用。一旦我从兼容性视图改回到IE9,它工作正常。在IE9(IE8和IE7)之前,也没有用。

How and when can these issues be fixed by Microsoft & Google?

微软和谷歌如何以及何时解决这些问题?

Any suggestions in the meantime to work around this?

在此期间有任何建议可以解决这个问题吗?

I unchecked the box under the Tools | compatability View Settings and unchecked the box that says "Display intranet sites in Compatability mode."

我取消选中工具|下的框兼容性查看设置并取消选中“在兼容模式下显示Intranet站点”框。

So, now when I go into IE, it automatically puts me into regular IE9 mode.

所以,现在当我进入IE时,它会自动进入常规的IE9模式。

Anyone know of a similiar setting for Chorme?

有人知道Chorme的类似设置吗?