0

I have a list defined in my controller. Now i want to get user input ,check if it exists in list in controller and then get all it related value.

Controller

private List<M> Ma()
   {
     List<M> md = new List<M>()
      { 
      new M{ R="a", A=300, T= 1, P=40, N=1200},
      new M{ R="Bl", A=100, T= 150, P=400, N=1200},
       };
      return mapData;
   }
9
  • Show the controller method for DashData() Commented Mar 10, 2015 at 4:40
  • Do you want to compare only with 'a' value? or with all values?? Commented Mar 10, 2015 at 4:46
  • The map is initialized in the same controller, so, it will be empty, and u will do forEach over nothing! Commented Mar 10, 2015 at 4:48
  • @AlBaraaSh Compare var a with every R in list, as all R in ilst will have unique value,so var a will match only one R from list. And then i have to get the next value of matching R. Commented Mar 10, 2015 at 4:48
  • 1
    Your ajax method is passing only one item { name : a } but the DashData() method does not even have a parameter named name. Then all you passing back is an empty list (and qv.val(data.name); makes no sense (typeof List<MapModel> does not contain a property named name) so a little hard to understand exactly what you wanting to return. Commented Mar 10, 2015 at 4:48

2 Answers 2

1

Firstly, change the ajax call to

var a = $('input').val();
var qv = $('.act');
var tv = $('.tot'); // you don't seem to use this
$.ajax({
  url: '@Url.Action("DashData", "Home")', // never hard code the url!
  type: "POST",
  data: { a: a }, // change this
  success: function (data) {
    // access the properties of the object
    var A = data.A; // returns 300
    var T = data.T; // returns 1
    var P = data.P; // returns 40
    var N = data.N; // returns 200
  }
});

Side notes:

  1. var a = $('input').val(); will always return the value of the first <input> in the view, so suggest you use the id attribute ($('#myInput').val();)
  2. You can also simplify it to $.post('Url.Action("DashData", "Home"), {a: a }, function(data) { ... })

Next modify the controller method to accept the parameter your passing and return the matching Map object

public ActionResult DashData(string a)
{
  // Get the first matching MapModel
    MapModel map = MapData().FirstOrDefault(m => m.R == a);
    return Json(map);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Your use of qv.val(data.name); made no sense so you may need to clarify exactly what you want to do with the values you return
I just want to set the values recieved in Vat T to another label in HTML
OK,so you should give the label an id attribute, and then $('#myLabel').text(data.T); (assuming by label, you mean an actual label - <label>SomeText</label>) For controls (input etc) use .val() and for other elements use .text()
Just one question. Do i set value in ajax success function or outside of ajax?
You might also want to check that you in fact returned a MapModel object - something like if(data) { // set the value here } else { // oops - there was no matching map }
|
1

Your controller should look like this:

public ActionResult DashData(double a)
{
    return Json(md.FirstOrDefault(map=>map.a == a));
}

While md is the List you defined first

then ur AJAX call should be like this:

    $.ajax({
            url: "../Home/DashData",
            type: "POST",
            data: {'a':a},
            success: function (data) {
                qv.val(data.a);
            }
        });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.