EasyE
EasyE

Reputation: 580

I am having issues sending data from a form post to my controller

I have an app that should accept 4 parameters from a form post which I am doing in my Index.cshtml class. I have my controller set to return a a csv file once it queries a database. I am using a form post and not sure what I am missing, because it is not sending any data to my controller in order to process the query and download the file.

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

    }

Upvotes: 0

Views: 45

Answers (1)

David
David

Reputation: 218877

None of your inputs have name attributes:

<input id="mrkGroup">

So the browser has no way of identifying them in the key/value pairs of POST values. Simply add some names:

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

Side note: Returning a FileResult object would be vastly preferred over directly writing to the Response output in MVC. You'll find it a lot easier to debug and test. Writing to the output and returning a view seems... error-prone.

Upvotes: 1

Related Questions