Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Implementing AJAX in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 67
    Comment on it

    While submitting or getting data from source like database or from file we can do it using the method of postback or we can do it using Ajax calls to minimize delay in our application.'

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    	Inherits="System.Web.Mvc.ViewPage" %>
    <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
    </asp:Content>
    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
    function getWeather() {
    var URL = "/Weather/GetWeather/" + $("#Location").val(); 
    $.get(URL, function(data) {
    $("#Result").html(data);
    }); 
    }
    </script>
    <div>
    <input type="text" id="Location" style="width: 325px" /> 
    <input type="button" id="btnSubmit" value="Get Weather" 
    	onclick="javascript:getWeather();" /> 
    <br /> <br />
    <div id="Result"></div>
    </div>
    </asp:Content>

    We can get weather details by making the ajax calls.

    Get method in ajax is used to make this call for fetching data.

    public class WeatherController : Controller
    {
    //
    // GET: /Weather/
    public ActionResult Index()
    {
    return View();
    }
    public ActionResult GetWeather(string Id)
    {
    StringBuilder sb = new StringBuilder(); 
    WeatherForecast wf = new WeatherForecast();
    WeatherForecasts wfs = wf.GetWeatherByPlaceName(Id);
    WeatherData[] wd = wfs.Details;
    sb.AppendFormat("<B>Weather Forecast for {0}</B><br /><br />", wfs.PlaceName);
    foreach (WeatherData d in wd)
    {
    if(!string.IsNullOrEmpty(d.WeatherImage))
    {
    sb.AppendFormat("<img src=\"{0}\" >", d.WeatherImage);
    sb.AppendFormat(" {0}", d.Day);
    sb.AppendFormat(", High {0}F", d.MaxTemperatureF);
    sb.AppendFormat(", Low {0}F<br />", d.MinTemperatureF);
    }
    }
    Response.Write(sb.ToString());
    return null; 
    }
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: