查看 MVC 中使用相同值填充的隐藏字段

发布于 2024-12-08 01:21:46 字数 5946 浏览 0 评论 0原文

我遇到了 MVC 3.0 的问题,这一定与我的代码有关,但我一直在更改刚刚离开公司的其他人的代码,并且似乎搞砸了一些我无法弄清楚的事情。我有一个看起来像这样的控制器:

直接报告提名

/// <summary>
/// Gets currently logged in user's direct reports nominees
/// </summary>
/// <returns></returns>
[HttpGet]
public ViewResult DirectReportNominees()
{
    this.nominationRepository = new NominationRowsRepository("Data Source=" + MvcApplication.SERVER_NAME + "; Initial Catalog=Cinet;Integrated Security=True",
                                                _currentuser.SAMAccountName);
    return View(nominationRepository.GetDirectReportNominees);
}

/// <summary>
/// Overload that triggers when supervisor clicks
/// Approve or Reject on Direct Report page
/// </summary>
/// <param name="submitButton"> button triggered to accept or Reject nomination</param>
/// <param name="NominationID">ID of nomination to change status of</param>
/// <returns></returns>
[HttpPost]
public ViewResult DirectReportNominees(string submitButton, string NominationID)
{
    this.nominationRepository = new NominationRowsRepository("Data Source=" + MvcApplication.SERVER_NAME + "; Initial Catalog=Cinet;Integrated Security=True",
                                                _currentuser.SAMAccountName);
    switch (submitButton)
    {
        case "Accept":

            NominationRow nomination = nominationRepository.GetNomination(NominationID);
            Employee nominatedUser = _adRepository.GetDirectoryUserInfo(nomination.NomineeLogin);

            // the email is sent from inside the repository
            nominationRepository.AcceptNomination(NominationID, nominatedUser);
            break;
        case "Reject":
            nominationRepository.RejectNomination(NominationID);
            break;
        default:
            break;
    }

    NominationID = "";
    return View(nominationRepository.GetDirectReportNominees);
}

控制器从数据库类收集与给定登录用户报告相关的各种提名并将其发送到下面的视图

视图看起来像这样:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<CINet.Areas.CAP.Models.CapDomain.Repositories.NominationStruct>>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
<%--<%: CINet.Views.Helpers.CssHelper.Css(Html, "~/Areas/CAP/Styles/Cap.css") %>--%>
<link rel="Stylesheet" type="text/css" href="<%: Url.Content("~/Areas/CAP/Styles/Cap.css") %>" />
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<div id="outsidewrapper">

<div id="picture"></div>

<div id="insidewrapper">
<div id="navcontainer">
    <% Html.RenderAction("Navigation", "Cap"); %>
</div>
<div id="CapsDashboard">
    <h3>Direct Report Nominees</h3>

        <table>
        <colgroup>
            <col id="nominee" />
            <col id="nominatedBy" />
            <col id="reason" />
            <col id="status" />
        </colgroup>
        <thead>
            <th scope="col">Name</th>
            <th scope="col">Nominated By</th>
            <th scope="col">Reason</th>
            <th scope="col">Status</th>
            <th></th>
        </thead>

       <tbody>
    <% foreach (var nomination in Model) { %>

                <tr>
                    <td><%: nomination.NomineeLogin %></td>
                    <td><%: nomination.NominatedBy %></td>
                    <td><%: nomination.NominationReason %></td>
                    <td><%: nomination.NominationStatus %></td>
                    <td>
                    <% if (nomination.NominationStatus == "Pending")
                       { %>
                       <%--Create Accept Button--%>
                       <% Html.BeginForm("DirectReportNominees", "Cap", FormMethod.Post); %>

                       <%: Html.Hidden("NominationID", nomination.NominationID.ToString()) %>
                       <input type="submit" name="submitButton" value="Accept" />

                       <% Html.EndForm(); %>

                       <%--Create Reject Button--%>
                       <% Html.BeginForm("DirectReportNominees", "Cap", FormMethod.Post); %>

                       <%: Html.Hidden("NominationID", nomination.NominationID.ToString()) %>
                       <input type="submit" name="submitButton" value="Reject" />

                       <% Html.EndForm(); %>

                    <% } %>

                    </td>
                </tr>

            <%--</div>--%>
    <% } %> 
    </tbody>
     </table>
     </div>
     </div>
     </div>
</asp:Content>

显然这是封装在一个站点管理员。视图接收模型并吐出有关指定用户的各种信息以及拒绝和批准按钮。该按钮值应设置为用户希望执行的特定操作,并且有一个隐藏的输入字段,该字段的值是通过数据库接收的提名 GUID。第一轮效果很好,但完成后,当视图从 HttpPost 返回时,所有隐藏值现在都具有相同的值 (GUID)。该值也总是与刚刚批准/拒绝的 GUID 或行相同。关于什么可以保持这个价值有什么想法吗?没有我可以看到的程序员使用的ViewData,并且视图中没有错误?当帖子返回时,我尝试单步执行视图的 foreach 循环,它们都是不同的,但是当它到达浏览器时,它们都是相同的值?非常奇怪而且很难确定。

有什么建议吗?

视图从中获取的类是:

 public class NominationStruct
    {
        public string NominationID; // unique ID in DB
        public string NomineeLogin;
        public string NominationReason;
        public string NominationReasonWithBreaks;
        public string NominatedBy;
        public string NominationStatus;

    }

模型是:

 public class NominationsListViewModel
    {
        public IList<NominationRow> NominationRows { get; set; }
        public string CurrentTab { get; set; }
    }

I have run into an issue with MVC 3.0 that must have something to do with my code, but I have been altering someone elses code that just left the company and seem to have screwed something up that I cannot figure out. I have a controller that looks like this:

Direct Report Nominees

/// <summary>
/// Gets currently logged in user's direct reports nominees
/// </summary>
/// <returns></returns>
[HttpGet]
public ViewResult DirectReportNominees()
{
    this.nominationRepository = new NominationRowsRepository("Data Source=" + MvcApplication.SERVER_NAME + "; Initial Catalog=Cinet;Integrated Security=True",
                                                _currentuser.SAMAccountName);
    return View(nominationRepository.GetDirectReportNominees);
}

/// <summary>
/// Overload that triggers when supervisor clicks
/// Approve or Reject on Direct Report page
/// </summary>
/// <param name="submitButton"> button triggered to accept or Reject nomination</param>
/// <param name="NominationID">ID of nomination to change status of</param>
/// <returns></returns>
[HttpPost]
public ViewResult DirectReportNominees(string submitButton, string NominationID)
{
    this.nominationRepository = new NominationRowsRepository("Data Source=" + MvcApplication.SERVER_NAME + "; Initial Catalog=Cinet;Integrated Security=True",
                                                _currentuser.SAMAccountName);
    switch (submitButton)
    {
        case "Accept":

            NominationRow nomination = nominationRepository.GetNomination(NominationID);
            Employee nominatedUser = _adRepository.GetDirectoryUserInfo(nomination.NomineeLogin);

            // the email is sent from inside the repository
            nominationRepository.AcceptNomination(NominationID, nominatedUser);
            break;
        case "Reject":
            nominationRepository.RejectNomination(NominationID);
            break;
        default:
            break;
    }

    NominationID = "";
    return View(nominationRepository.GetDirectReportNominees);
}

The Controllergathers from a Database class The various nominations that relate to a given logged in users report to and sends it to the below View

The View looks like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<CINet.Areas.CAP.Models.CapDomain.Repositories.NominationStruct>>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
<%--<%: CINet.Views.Helpers.CssHelper.Css(Html, "~/Areas/CAP/Styles/Cap.css") %>--%>
<link rel="Stylesheet" type="text/css" href="<%: Url.Content("~/Areas/CAP/Styles/Cap.css") %>" />
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<div id="outsidewrapper">

<div id="picture"></div>

<div id="insidewrapper">
<div id="navcontainer">
    <% Html.RenderAction("Navigation", "Cap"); %>
</div>
<div id="CapsDashboard">
    <h3>Direct Report Nominees</h3>

        <table>
        <colgroup>
            <col id="nominee" />
            <col id="nominatedBy" />
            <col id="reason" />
            <col id="status" />
        </colgroup>
        <thead>
            <th scope="col">Name</th>
            <th scope="col">Nominated By</th>
            <th scope="col">Reason</th>
            <th scope="col">Status</th>
            <th></th>
        </thead>

       <tbody>
    <% foreach (var nomination in Model) { %>

                <tr>
                    <td><%: nomination.NomineeLogin %></td>
                    <td><%: nomination.NominatedBy %></td>
                    <td><%: nomination.NominationReason %></td>
                    <td><%: nomination.NominationStatus %></td>
                    <td>
                    <% if (nomination.NominationStatus == "Pending")
                       { %>
                       <%--Create Accept Button--%>
                       <% Html.BeginForm("DirectReportNominees", "Cap", FormMethod.Post); %>

                       <%: Html.Hidden("NominationID", nomination.NominationID.ToString()) %>
                       <input type="submit" name="submitButton" value="Accept" />

                       <% Html.EndForm(); %>

                       <%--Create Reject Button--%>
                       <% Html.BeginForm("DirectReportNominees", "Cap", FormMethod.Post); %>

                       <%: Html.Hidden("NominationID", nomination.NominationID.ToString()) %>
                       <input type="submit" name="submitButton" value="Reject" />

                       <% Html.EndForm(); %>

                    <% } %>

                    </td>
                </tr>

            <%--</div>--%>
    <% } %> 
    </tbody>
     </table>
     </div>
     </div>
     </div>
</asp:Content>

obviously this is encapsulated inside a SiteMaster. The View takes in the model and spits out the various information regarding the user nominated along with a Reject and Approve Button. This buttons value should be set to the particular thing the user wishes to do and there is a hidden input field thats value is the Nomination GUID which is recieved via the database. The first round works great, but after that is completed, when the view returns from the HttpPost, all the hidden values now have the same value (GUID). The value happens to also always be the same as the GUID or row that was just approved/rejected. Any ideas on what could be holding this value? There is no ViewData that the programmer used that I can see and no errors in the View? I have tried stepping through the View's foreach loop when the post returns and they are all different, but by the time it gets to the browser, they are all the same value?? Very strange and hard to pin point.

Any suggestions??

The Class that the View is getting from is:

 public class NominationStruct
    {
        public string NominationID; // unique ID in DB
        public string NomineeLogin;
        public string NominationReason;
        public string NominationReasonWithBreaks;
        public string NominatedBy;
        public string NominationStatus;

    }

And the Model is:

 public class NominationsListViewModel
    {
        public IList<NominationRow> NominationRows { get; set; }
        public string CurrentTab { get; set; }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2024-12-15 01:21:46

我的问题通过另一篇文章得到了解答。

这是默认行为。请参阅

http://forums.asp.net/p/1559541/3846605.aspx

I got my question answered through another post.

This is the default behaviour. See

http://forums.asp.net/p/1559541/3846605.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文