Tuesday, July 19, 2016

How to add multiple css values into Style in MVC

@{
string color = "black";
if (Model[i].RevenueRecovered.HasValue && Model[i].RevenueRecovered.Value < 0)
{
color = "red";
       }
}

new { @style = string.Format("width:100%; color:{0}", @color) }

Wednesday, February 17, 2016

Async and Await


Controller Side Implementation

[OutputCache(Duration = 0)]
 public async Task<JsonResult> BindAll()
 {
var Task1 = _res.Task1().ConfigureAwait(false);
var Task2 = _res.Task2().ConfigureAwait(false);
var Task3 = _res.Task3().ConfigureAwait(false);
var resTask1 = (await Task1).OrderBy(t => t.Id).ToList();
var resTask2 = (await Task2).OrderBy(t => t.Id).ToList();
var resTask3 = (await Task3).OrderBy(t => t.Id).ToList();

return Json(new
{
resTask1,
     resTask2,
     resTask3
}, JsonRequestBehavior.AllowGet);

}

Web.API Implementation

public async Task<List<ClassName>> GetAll(int value)
{
var apiUrl = string.Format("{0}/api/ControllerName/GetAll/{1}", _ServiceName, value);
       using (var client = new HttpClient())
       {
           var response = await client.GetAsync(apiUrl);
              if (!response.IsSuccessStatusCode)
                   return null;
                return await response.Content.ReadAsAsync<List<ClassName>>();
       }
}


[Route("GetAll/{value}")]
public IHttpActionResult GetAll(int value)
{
var results = _service.GetAll(value);
       if (results == null || results.Count == 0)
           return NotFound();
            else
              return Ok(results);
}


public async Task<List<ClassName>> GetAllValues(Model objFilter)
{
var apiUrl = string.Format("{0}/api/ControllerName/GetAllValues/", _ServiceName);
       using (var client = new HttpClient())
       {
           var response = await client.PostAsJsonAsync(apiUrl, objFilter);
              if (!response.IsSuccessStatusCode)
                  return null;
              return await response.Content.ReadAsAsync<List<ClassName>>();
       }
}


[HttpPost]
[Route("GetAllValues")]
public IHttpActionResult GetAllValues(Model objFilter)
{
var results = _service.GetAllValues(objFilter);
       if (results == null || results.Count == 0)
            return NotFound();
       else
            return Ok(results);
}

Wednesday, July 29, 2015

How to change the name of the button using Jquery

<input type='button' value='Add' id='btnAddProfile'>
$("#btnAddProfile").attr('value', 'Save'); //versions older than 1.6

<input type='button' value='Add' id='btnAddProfile'>
$("#btnAddProfile").prop('value', 'Save'); //versions newer than 1.6

<button id='btnAddProfile' type='button'>Add</button>
$("#btnAddProfile").html('Save');

How to Make Checkbox checked using Jquery

$('input:checkbox[id=2]').prop('checked', true);

How to change/modify values in list of list in C#

ObjList.ClassList.ForEach(x => x.Id =0); // It will change all Id's to 0

How to validate table column values as Row level

function ValidateTableTDInputValues() {
$('input').removeClass('error');
       $('span.mandatory').remove();
       var res = true;
       this.$('.ClassName1, .ClassName2').each(function () {
       var value = '';
       value = $(this).val();
       var tableRow = $(this).parent().parent();
       if (value != "") {
       var txtBoxOneInput = tableRow.find("td:nth-child(1) input[type=text]");
       var txtBoxOneValue = txtBoxOneInput.val();
       var txtBoxTwoInput = tableRow.find("td:nth-child(2) input[type=text]");
var txtBoxTwoValue = txtBoxTwoInput.val();
       var txtBoxThreeInput = tableRow.find("td:nth-child(3) input[type=text]");
       var txtBoxThreeValue = txtBoxThreeInput.val();
       var txtBoxFourInput = tableRow.find("td:nth-child(4) input[type=text]");
       var txtBoxFourValue = txtBoxFourInput.val();

       if (!$(txtBoxOneInput).hasClass('error') && $.trim(txtBoxOneValue) == '') {
       $(txtBoxOneInput).addClass('error').after('<span class="mandatory">*</span>');
res = false;
       }
      
if (!$(txtBoxTwoInput).hasClass('error') && $.trim(txtBoxTwoValue) == '') {
       $(txtBoxTwoInput).addClass('error').after('<span class="mandatory">*</span>');
res = false;
}
      
if (!$(txtBoxThreeInput).hasClass('error') && $.trim(txtBoxThreeValue) == '') {
       $(txtBoxThreeInput).addClass('error').after('<span class="mandatory">*</span>');
res = false;
       }
if (!$(txtBoxFourInput).hasClass('error') && $.trim(txtBoxFourValue) == '') {
       $(txtBoxFourInput).addClass('error').after('<span class="mandatory">*</span>');
res = false;
}
    }
});
 if (res) { return true; } else { return false; }
}

How to convert DbContext list into our local class List


List<Tempclass> objList = new List<Tempclass>();
var objValues = DbContext.Values.Where(val => (val.Id == Id) && (val.IsObsolute == (Boolean?)false));

if (objValues != null && objValues.Any())
{
    objList = (from Value res in objValues
              select new Tempclass()
              {
   Id = res.Id,
                Name = res.Name,
               Age = res.Age,
               Sex = res.Sex
              }).ToList();
 }
 return objList;