0

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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Do I need to declare the model at the top of the view like I did for the drop down lists?
Yea you do so it would be @models yourNameSpace.FolderName.ModelName
Im new to asp.net mvc so im wondering how the Jquery you added will know to post the data to #PartialID when it's in a different view. Also wouldn't I declare my model as @model IList <namespace.modelfolder.model>?
Also my model is performing a method that takes in the 2 timestamps so do I call the method at the end of the model call in the view? I see that you can do @renderpartial ("~/partialviewlocation", what do I put here) my model returns a list of another model class with the 4 fields so which one do I want to return
You need to reference the partial view on your view ` @Html.Partial("~/Path/to/PartialView/Partial.cshtml", Model.Parts)` . This will add the contents of your partial to your view, making them one view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.