【问题标题】:MVC 4 Pass parameter from ActionLink (type: HttpGet) to ControllerMVC 4将参数从ActionLink(类型:HttpGet)传递到控制器
【发布时间】:2016-10-17 09:38:33
【问题描述】:

我是 Asp.net MVC 的新手,目前我想在用户单击操作链接按钮时将一些参数(选定的日期)传递给控制器​​,下面是操作链接和日期选择器字段代码,

   <div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">
            Manage Outward - Completed</h3>
    </div>
    <div class="panel-body">
        @Html.CreateJQGrid(Model.GridCompletedOWData)
        <br />
        <div class="icon-list">
            @Html.ActionLink("Download Completed Transactions", "ExportToExcelOutwardCompleted", "InwardOutward", new { enctype = "multipart/form-data", model = @Model, FromDate = @Model.DownloadOutwardFromDate.ToString(), ToDate = Model.DownloadOutwardFromDate.ToString() }, new { @id = "DownloadAccount", @class = "linkTxt" })
        </div>
    </div>
       <div class="col-md-3">
                <label for="" class="control-label">
                    Download From Date
                </label>
                @Html.EditorFor(model => model.DownloadOutwardFromDate, new { @class = "form-control" }).DisableIf(() => Model.IsReadOnly == true)
                @Html.HiddenFor(model => model.DownloadOutwardFromDate)
        </div>
        <div class="col-md-3">
            <label for="" class="control-label">
                Download To Date
            </label>
            @Html.EditorFor(model => model.DownloadOutwardToDate, new { @class = "form-control" }).DisableIf(() => Model.IsReadOnly == true)
            @Html.HiddenFor(model => model.DownloadOutwardToDate)
        </div>
        <br />
</div>

还有我的控制器,

  public ActionResult ExportToExcelOutwardCompleted(CompletedIWOWDetailsModel model, string FromDate, string ToDate)
    {

        var employeeBranch = employeeBranchService.FilterBy(x => x.Userseq == LoggedInUser.UserSeqeunce).Select(x => x.Branchseq).ToList();
        var userIds = employeeBranchService.FilterBy(x => employeeBranch.Contains(x.Branchseq)).Select(x => x.Userseq).ToList();

        List<TrnOutflowDetailsAud> trnInflowDetailsP = trnOutwardDetailsAudService.FilterBy(x =>
            (employeeBranch.Contains(x.InitiatedBranchSeq) && (x.OutflowStage == (int)enumOutflowStage.Completed) && x.Transactiondate >= DateTime.Now.AddDays(-30))).ToList();

        var clientDCAList = clientDcaService.All().Where(x => x.Active == "Y").ToList();
        var List = (from p in trnInflowDetailsP.ToList()
                    join C in GetCurrency() on p.Currencyoftheinstructedamount equals C.Id into CTClist
                    from C in CTClist.DefaultIfEmpty()

                    join CC in GetCurrency() on p.Currencyofthetransaction equals CC.Id into CTC
                    from CC in CTC.DefaultIfEmpty()

                    join PC in GetPurposeCode() on p.PurposeCodeSeq equals PC.Id into PurLIST
                    from PC in PurLIST.DefaultIfEmpty()

                    join CU in GetUserList() on p.CreateUser equals CU.Id into CUL
                    from CU in CUL.DefaultIfEmpty()

                    join AU in GetUserList() on p.AuthUser equals AU.Id into AUL
                    from AU in AUL.DefaultIfEmpty()

                    //join D in GetDepartmentList() on p.DepartmentSeq equals D.Id into DEPList
                    //from D in DEPList.DefaultIfEmpty()

                    join b in branchService.All().ToList() on p.BranchSeq equals b.Id into branch
                    from b in branch.DefaultIfEmpty()

                    join IB in branchService.All().ToList() on p.InitiatedBranchSeq equals IB.Id into IBList
                    from IB in IBList.DefaultIfEmpty()

                    //join CTC in clientDCAList on p.AccountNumber equals CTC.AccountNo into CTCList
                    //from CTC in CTCList.DefaultIfEmpty()

                    select new
                    {
                        p.TranRef,
                        TradeStatus = p.OutflowStage == 0 ? "" : ((enumOutflowStage)p.OutflowStage).GetDescriptionEnum(),
                        InitiatedBranchName = IB == null ? "" : IB.BranchName,
                        p.Draccountnumber,
                        BranchName = b == null ? "" : b.BranchName,
                        TransactionDate = p.Transactiondate.GetFormatDateWithOutTime(),
                        p.CustomerName,
                        p.Noofinstructions,
                        p.Amount,
                        CurrencyOfTheInstructedAmount = C == null ? "" : C.CurrencyCode,
                        CurrencyOfTheTransaction = CC == null ? "" : CC.CurrencyCode,
                        PurposeCode = PC == null ? "" : PC.PurCode,
                        ProductName = p.ProductName == 0 ? "" : ((enumOutflowProductNames)p.ProductName).GetDescriptionEnum(),
                        p.ProductNameOthers,
                        p.Anyotherspecificinstruction,
                        Modeoftransaction = p.Modeoftransaction == null ? "" : ((enumModeOfTransaction)p.Modeoftransaction).GetDescriptionEnum(),
                        SignatureVerification = p.SignatureVerification.GetYesNoString(),
                        Callback = p.Callback.GetYesNoString(),
                        p.IdmsRefNo,
                        //p.BatchNo,
                        p.Remarks,
                        Active = p.Active.GetYesNoString(),
                        //p.Status,
                        CreateUserName = CU == null ? "" : CU.UserName,
                        CreateDate = p.CreateDate.GetFormatDateWithOutTime(),
                        p.CreatorRemarks,
                        AuthUserName = AU == null ? "" : AU.UserName,
                        AuthDate = p.AuthDate.GetFormatDateWithOutTime(),
                        p.AuthRemarks
                    }).ToList();

        ExcelPackage excel = new ExcelPackage();
        var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
        workSheet.Cells[1, 1].LoadFromCollection(List, true);
        using (var memoryStream = new MemoryStream())
        {
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=" + Constants.Controller.InwardOutward + ".xlsx");
            excel.SaveAs(memoryStream);
            memoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }
        return View();
    }

现在我想将选定的日期 (DownloadOutwardFromDate) 作为参数传递给 ExportToExcelOutwardCompleted 方法。但是每当我单击 actionlink 按钮时,FromDate 参数为空或默认日期值,无法获取选定的值。所以请任何一个给出解决方案

【问题讨论】:

  • 这个参数不需要传递,你传递的是模型,然后是其中的两个变量,为什么不直接传递模型并在控制器中提取这些变量?
  • 我不知道如何将模型作为参数传递,因为如果我想传递模型,那么我应该使用 HttpPost 类型对吗?而且我们也不能通过使用 ActionLink 来做到这一点吗? BeginForm 用于发布模型,所以我想使用 ActionLink。如果您知道如何将模型作为 ActionLink 的参数传递,请帮我编写一些代码。
  • 我的意思是,在您的操作链接中,objectVariables 具有model = @Model,因此您已经将模型作为参数传递。之后你有FromDate = @Model.DownloadOutwardFromDate.ToString()。在这种情况下,您的控制器中可以有代码从您正在执行的模型中获取代码
  • @MdAslam 您已经在 ActionLink 中将模型作为参数传递了,所以也许您确实知道该怎么做!您没有必须使用 POST 来传递整个对象,只是查询字符串的大小存在限制,并且您冒着通过发送大对象来达到该限制的风险。无论如何,在这种情况下,由于您发送的 FromDate 已经是模型的属性(您也在发送),因此将其作为另一个单独的参数发送似乎是多余的。
  • 是的,我尝试过这种方式来获取日期值,但我无法获取该值。 model = @Model 和 FromDate = @Model.DownloadOutwardFromDate.ToString() 都是空的,所以请帮帮我

标签: c# asp.net-mvc-4 razor http-get html.actionlink


【解决方案1】:

将模型直接传递给动作。在代码隐藏的操作中,直接读取这些字段。

修改动作的签名,使其可以作为模型的参数。

【讨论】:

  • @MdAslam 有很多例子。你总是对那些不在你的地方写代码的人进行负面标记?
【解决方案2】:

要提交用户输入的值,您必须使用formform 可以设置为 POSTGET

示例

景色

@using (@Html.BeginForm("ExportToExcelOutwardCompleted", "InwardOutward", FormMethod.Get))
{
    @Html.TextBox("dateFrom")
    @Html.TextBox("dateTo")
    <button type="submit">Submit</button>
}

控制器动作

public ActionResult ExportToExcelOutwardCompleted(string dateFrom, string dateTo)
{
    // Your code here

    return View();
}

文本框的 id 设置为与操作方法中的参数相同的名称。这样模型绑定才能正确关联请求中的数据。

【讨论】:

    猜你喜欢
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多