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

时间:2011-10-03 20:40:04

标签: asp.net-mvc submit-button

我遇到了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);
}

来自数据库类的Controllergathers与给定登录用户相关的各种提名报告并将其发送到以下视图

View看起来像这样:

<%@ 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>

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

有什么建议吗?

View来自的类是:

 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; }
    }

1 个答案:

答案 0 :(得分:0)

我通过另一篇文章回答了我的问题。

这是默认行为。参见

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