BviLLe_Kid BviLLe_Kid - 4 months ago 20
Ajax Question

Button isn't sending to correct view

I am using ajax to send values to my controller from the view:

View where information is collected to send to a different controller:

@using (Html.BeginForm(null, null, FormMethod.Get, htmlAttributes: new { id = "GenerateForm" }))
{
<div class="form-horizontal">
<div class="form-group">
@Html.Label("Choose AC:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AC", null, "-- Select AC --", htmlAttributes: new { id = "AC", @class = "form-control" })
</div>
</div>
</div>


<div class="form-horizontal">
<div class="form-group">
@Html.Label("Choose Month:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Months", null, "-- Select Month --", htmlAttributes: new { id = "Month", @class = "form-control" })
</div>
</div>
</div>

<div class="form-horizontal">
<div class="form-group">
@Html.Label("Year:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Years", null, "-- Select Year --", htmlAttributes: new { id = "Year", @class = "form-control" })
</div>
</div>
</div>

<br />

<input type="submit" id="SendToController" class="btn btn-primary" value="Generate" />
}


<script type="text/javascript">
$('#SendToController').on('click', function() {
sendToController();
return false;
});
function sendToController(){
var selectedAC = $('#AC').val();
var selectedMonth = $('#Month').val();
var chosenYear = $('#Year').val();
$.ajax({
url: '/MonthReports/Generate',
data: { 'id' : selectedAC, 'monthValue' : selectedMonth, 'year' : chosenYear },
type: 'GET',
cache: false,
success: function(data){},


});
}




Controller Method:

public ActionResult Generate(int id, int monthValue, string year)
{
List<DailySum> lstDailySum = db.DailySum.Where(x => x.AID == id && x.Day.Month == monthValue + 1 && x.Day.Year.ToString() == year && x.deleted == false).ToList();
List<string> lstInc = lstDailySum.Select(x => x.codeAC.text).Distinct().ToList();
List<MReport> lstMReport = new List<MReport>();

foreach (var inc in lstInc)
{
MReport mReport = new MReport();
mReport.Inc = inc;
mReport.Count = lstDailySum.Where(x => x.codeAC.text == incident).Count();
lstMReport.Add(mReport);

}

return View(lstMReport);
}


Now the values are being passed through when the button is clicked, and the whole method works, except the View doesn't show up.. it just stays on the View where the button was originally clicked... no Generate View appears.

I have placed a breakpoint on the Generate view and it does get hit but the view doesn't show?

I don't know how else to explain it.. the cshtml code is hit with a breakpoint, but the page doesn't show.. it just stays on the page where the button was clicked.

Any help is appreciated.

Answer

Because you are using ajax, you should handle the data in the success callback of your ajax call. The way your code is written, you do nothing with the view. The View is rendered to the data variable.

Try something like this:

$.ajax({
    url: '/MonthReports/Generate',
    data: { 'id' : selectedAC, 'monthValue' : selectedMonth, 'year' : chosenYear },
    type: 'GET',
    cache: false,
    success: function(data){
        $("body").html(data);
    }
});
Comments