-1

I am new to Asp.net MVC and currently I want to pass some parameter (selected dates) to the controller when user click the action link button, below is the action link and date picker field code,

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

And my controller,

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

Now I want to pass the selected date (DownloadOutwardFromDate) as a parameter to the ExportToExcelOutwardCompleted method.But whenever I click the actionlink button then the FromDate parameter is null or default date value, not able to get selected value.So please any one give solution

Md Aslam
  • 1,228
  • 8
  • 27
  • 61
  • This parameter doesn't need to be passed, you're passing the model and then two variables from it afterwards, why not just pass the model and extract those variables in your controller? – Alfie Goodacre Oct 17 '16 at 09:42
  • I don't know how to pass model as parameter because if i want to pass model then i should use HttpPost type right ? And also we can not do it by using ActionLink right?. BeginForm is used to post the model, so I want to use ActionLink. If you know how to pass model as parameter for ActionLink please help me with some code. – Md Aslam Oct 17 '16 at 09:48
  • What I mena is that in your actionlink, the objectVariables has `model = @Model`, so you're already passing the model as a parameter. After that you have `FromDate = @Model.DownloadOutwardFromDate.ToString()`. In this case you could have code in your controller that gets that from the model you're psasing – Alfie Goodacre Oct 17 '16 at 09:50
  • @MdAslam you are already passing the model as a parameter in your ActionLink, so maybe you do know how to do it! You don't _have_ to use a POST to pass an entire object, it's just that there is a limit to the size of a querystring and you run the risk of hitting that limit by sending a large object. Anyway in this case, since the FromDate you are sending is already a property of the model (which you are also sending), then sending it as another separate parameter appears to be redundant. – ADyson Oct 17 '16 at 09:51
  • Yes, I tried that way to get the date value, but I can not get the value. model = @Model and FromDate = @Model.DownloadOutwardFromDate.ToString() both are null only, So please help me – Md Aslam Oct 17 '16 at 09:52
  • You cannot use `model = @Model` (look at the html of the link you generating to understand). But what would be the point of passing back the original values you sent to the view. If you want to make a GET, wrap the form controls in a `BeginForm()` with `FormMethod.GET` (and remove the pointless hidden inputs) –  Oct 17 '16 at 09:58

2 Answers2

-1

Pass the model directly to the action. In the action in the codebehind, read those fields directly.

Modify the signature of the action so it can take as parameter the model.

Liquid Core
  • 1
  • 6
  • 27
  • 52
  • @MdAslam There are plenty of examples out there. You always flag negatively everyone who doesn't write code at your place? – Liquid Core Oct 21 '16 at 09:26
-1

To submit values entered by a user you must use a form. A form can be set to POST or GET.

Example

The view

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

The Controller action

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

    return View();
}

The textboxes have an id set to the same name as the parameter in the action method. This is so that the model binding can associate the data in the request correctly.

Colin Bacon
  • 15,436
  • 7
  • 52
  • 72