我在将表格帖子中的数据发送到控制器时遇到问题

时间:2016-02-16 19:21:48

标签: c# asp.net-mvc razor controller

我有一个应用程序应该接受来自我在Index.cshtml类中执行的表单发布中的4个参数。我将控制器设置为在查询数据库后返回一个csv文件。我正在使用表单帖子而不确定我缺少什么,因为它没有向我的控制器发送任何数据以处理查询和下载文件。

This is my HTML 
<div id="engagementForm" class="col-lg-offset-4">
@using (Html.BeginForm ("GetClientList", "Home", FormMethod.Post) )
{   
    <div class="form-horizontal" role="form">
        <div class="form-group" id="marketSelection">
            <label class="control-label col-sm-2" name ="marketGroup" id="marketGroup">Engagement Market:</label>
            <div class="col-lg-10">
                <input id="mrkGroup">
            </div>
        </div>
        <div class="form-group" id="officeSelection">
            <label class="control-label col-sm-2" name ="engagementOffice" id="engagementOffice">Engagement Office:</label>
            <div class="col-sm-10">
                <input id="engOffice">
            </div>
        </div>
        <div class="form-group" id="partnerSelection">
            <label class="control-label col-sm-2" Name ="engagementpartner" id="engagementpartner">Engagement Partner:</label>
            <div class="col-sm-10">
                <input id="engPartner">
            </div>
        </div>
        <div class="form-group" id="statusSelection">
            <label class="control-label col-sm-2" Name ="engagementStatus" id="engagementStatus">Engagement Status:</label>
            <div class="col-sm-10">
                <input id="engStatus">
            </div>
        </div>
        <button class="k-button" type="submit" id="searchButton">Create Speardsheet</button>
    </div>

}

This is my Controller 
public ActionResult GetClientList(int? marketGroup, int? engagementOffice, int? engagementpartner, int? engagementStatus)
    {
        List<Engagement> QueryResult = PMService.GetRequestedEngagments(marketGroup, engagementOffice, engagementpartner, engagementStatus);        


        var writetofile = PMService.BuildCsvString(QueryResult);
        var bytefile = Encoding.UTF8.GetBytes(writetofile);


        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
        Response.Output.Write(writetofile);
        Response.Flush();
        Response.End();         

        return View();

    }

1 个答案:

答案 0 :(得分:1)

您的所有输入都没有name属性:

<input id="mrkGroup">

因此浏览器无法在POST值的键/值对中识别它们。只需添加一些name s:

<input id="mrkGroup" name="marketGroup">

附注:在直接写入MVC中的FileResult输出时,返回Response对象非常优先。您会发现调试和测试更容易。写入输出返回视图似乎......容易出错。