Booza
Booza

Reputation: 126

.net mvc jquery to sql parameter

I am creating an MVC web application in .net that has 2 drop down lists with a list of timestamps. I am able to populate those drop downs successfully using linq query from the model of IEnumerable<> (.edmx).

@model IEnumerable<Softwares>
@{ var times = from ts in Model select ts.Distinct() }

<select id="time1">
@foreach(var item in times)
  { <option> @item </option> }
</select>

<select id="time2">
@foreach(var item in times)
  { <option> @item </option> }
</select>


<button id="compare> Compare </button>

I need to take these 2 timestamps as parameters into an sql query. When debugging it seems that I am able to pass the timestamps back to the Action Result in my controller, into the model to as parameters and then back to my Controller.

<script> 
var t1 = $("#time1").val();
var t2 = $("#time2").val();
$(document).ready(function(){
  $("#compare").click(){
      $.post("Home/CompareSoftware", {time1 = t1, time2 = t2},
          function(data){
             alert(data)
        });
 </script>

My problem is getting the value from the controller and pushing it back to the view formatted as a table. I'm not quite sure what the "data" is because it seems like it is my t1 and t2 that I'm passing back to my Controller action CompareSoftware. I try just getting it to show up in an alert box but when I run this script and click compare I get an unsuccessful message. Please note that I am writing this code from memory so it might not be exactly correct here.

Here is my controller:

public ActionResult CompareSoftware(string time1, string time2){

time1 = t1; time2 = t2; Convert to TimeStamp.

var retVal = Models.CompareSoftware(t1, t2)

return View(retVal) 
}

My model takes in 2 timestamps and returns a List class that has 4 fields. My code is paraphrased above but when debugging I am able to get a return value from my model and push it back to the Controller. How to I display this information to the view? I'm guessing this ajax post returns JSON data and I need to convert that to my view? I'm not exactly sure.

Upvotes: 0

Views: 147

Answers (1)

joetinger
joetinger

Reputation: 2729

Your alert isn't showing data because you aren't referencing the individual item in the list. Add this to your JQuery

function(data){
   $.each(data, function (index, item) {
       alert(item);
   });
});

I'm assuming you're trying to return the results on the same page? If so try this:

Controller: return a Partial View

 return PartialView("YourPartialViewFolder/YourPartialView", Your_List);

JQuery: Returns the table to the Partial

 $.post("Home/CompareSoftware", {time1 = t1, time2 = t2},
      function(data){
         $('#PartialID').html(data)
    });

Partial View Here you would just have an empty table until it is clicked. So I would hide it, unless you want an empty table to show.

<table id="PartialID">
  <tr>
    <thead>
      <th>Header Title</th>
    </thead>
  </tr>
  @foreach(var item in Model)
  {
    <tr>
      <tbody>
        <td>@Html.DisplayFor(x => item.ListItem)</td>
      </tbody>
    </tr>
  }
</table>

Upvotes: 1

Related Questions