[英]MVC 4 Pass parameter from ActionLink (type: HttpGet) to Controller
我是 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 方法。
將模型直接傳遞給動作。 在代碼隱藏的操作中,直接讀取這些字段。
修改動作的簽名,以便它可以將模型作為參數。
要提交用戶輸入的值,您必須使用form
。 form
可以設置為POST
或GET
。
例子
風景
@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 設置為與操作方法中的參數相同的名稱。 這是為了讓模型綁定可以正確關聯請求中的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.