2013-08-06 84 views
2

控制器的方法当用户点击Html.ActionLink,我需要调用一个控制器的方法,将下载该用户的csv报告。我还需要将这个控制器的值从两个输入框中传递出来,它们表示他们正在查找的开始日期和结束日期范围。传递输入参数,通过Html.ActionLink

目前我可以指定使用jQuery的Html.ActionLink参数,但是他们没有把它回控制器。控制器方法中的两个参数都使用null值进行实例化。

我也不能使用表单/提交方式为已经被这种特殊的形式来让用户看到导出到CSV之前要求在日期范围内的数据。

的jQuery

$(document).ready(function() { 
    $('#startDate').change(function() { 
     $('a').attr('start', $(this).val()); 
    }); 

    $('#endDate').change(function() { 
     $('a').attr('end', $(this).val()); 
    }); 
}); 

ASP MVC 3查看

@using (Html.BeginForm()) 
{ 
    <div id="searchBox"> 
     @Html.TextBox("startDate", ViewBag.StartDate as string, new { placeholder = " Start Date" }) 
     @Html.TextBox("endDate", ViewBag.EndDate as string, new { placeholder = " End Date" }) 
     <input type="image" src="@Url.Content("~/Content/Images/Search.bmp")" alt="Search" id="seachImage"/> 
     <a href="#" style="padding-left: 30px;"></a> 
    </div> 
    <br /> 
    @Html.ActionLink("Export to Spreadsheet", "ExportToCsv", new { start = "" , end = ""}) 
    <span class="error"> 
     @ViewBag.ErrorMessage 
    </span> 
} 

控制器方法

public void ExportToCsv(string start, string end) 
    { 

     var grid = new System.Web.UI.WebControls.GridView(); 

     var banks = (from b in db.AgentTransmission 
        where b.RecordStatus.Equals("C") && 
          b.WelcomeLetter 
        select b) 
        .AsEnumerable() 
        .Select(x => new 
           { 
            LastName = x.LastName, 
            FirstName = x.FirstName, 
            MiddleInitial = x.MiddleInitial, 
            EffectiveDate = x.EffectiveDate, 
            Status = x.displayStatus, 
            Email = x.Email, 
            Address1 = x.LocationStreet1, 
            Address2 = x.LocationStreet2, 
            City = x.LocationCity, 
            State = x.LocationState, 
            Zip = "'" + x.LocationZip, 
            CreatedOn = x.CreatedDate 
           }); 


     grid.DataSource = banks.ToList(); 
     grid.DataBind(); 

     string style = @"<style> .textmode { mso-number-format:\@; } </style> "; 

     Response.ClearContent(); 
     Response.AddHeader("content-disposition", "attachment; filename=WelcomeLetterOutput.xls"); 
     Response.ContentType = "application/excel"; 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter htw = new HtmlTextWriter(sw); 
     grid.RenderControl(htw); 
     Response.Write(style); 
     Response.Write(sw.ToString()); 
     Response.End(); 
    } 

回答

3

我认为问题在于链接没有“开始”或“结束”属性。所以$('a').attr('start', $(this).val());不会做任何事情。

<a href="#" id="lnkExport">Export to Spreadsheet</a> 

$('#lnkExport').click(function (e){ 
e.preventDefault(); 
window.location = '/Home/ExportToCsv?start=' + $('#startDate').val() + '&end=' + $('#endDate').val(); 
}); 
+0

谢谢!我使用''@ Url.Action(“ExportToCsv”,“Agent”)'来获取URL,但这正是我所期待的。谢谢! – NealR

+0

很高兴为你效劳! –

1

我也不能使用表单/提交方法作为已经被使用 这种特殊形式以允许用户看到导出到CSV之前所要求的日期 范围内的数据。

实际上,您可以在窗体内部提交2个按钮,并在您的POST控制器操作中知道哪个按钮被点击以执行相应操作。就这样:

<button type="submit" name="btn" value="search">Search</button> 
<button type="submit" name="btn" value="export">Export to Spreadsheet</button> 

现在你的控制器动作可以采取btn说法:

[HttpPost] 
public ActionResult SomeAction(MyViewModel model, string btn) 
{ 
    if (btn == "search") 
    { 
     // The search button was clicked, so do whatever you were doing 
     // in your Search controller action 
    } 
    else if (btn == "export") 
    { 
     // The "Export to Spreadsheet" button was clicked so do whatever you were 
     // doing in the controller action bound to your ActionLink previously 
    } 

    ... 
} 

正如你可以在表单被提交到同一个控制器的动作中,你会明显得到这两种情况下看您的视图模型从表单数据,除此之外,你会知道哪个按钮被点击,以采取适当的行动。所以现在你可以摆脱所有你可能写的花哨的JavaScript和主播。即使用户禁用了JavaScript,您的应用程序也能正常工作。这是简单的,简单的HTML。

注意:请记住,你已经显示和调用控制器方法在你的问题是不是在ASP.NET MVC的标准。在ASP.NET MVC中,这些方法有名称。 Tjeir被称为控制器操作,并且必须返回ActionResults而不是无效的。您认为在ASP.NET MVC应用软件中,您好像使用var grid = new System.Web.UI.WebControls.GridView();

+0

这确实是一个黑客,你会建议什么? – NealR

+0

使用OpenXML SDK生成Excel文件:http://www.microsoft.com/en-us/download/details.aspx?id = 5124 –

+0

这是我们必须在部署之前在服务器上安装的东西,还是我只需要在本地计算机上执行一次,然后将其添加到控制器? – NealR