Ajax retrieve JSON data and Html data from MVC Controllers in ASP.NET MVC

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   java   使用   io   ar   

一、

準備好後台測試資料,過程略

 

二、用戶端處理

@{    ViewBag.Title = "Index";}<h2>Ajax Demo</h2><div style="width: 600px; ">    <div style="background-color:lightgray">        <h2>Meals</h2>    </div>    <p>Click the button to Get Meals with an Ajax call</p>    <input id="btnAjax" name="btnAjax" type="button" value="Get Meals" />    <div id="meals" style="background-color:lightskyblue"></div>    <div id="jsonResult" style="background-color:lightsalmon"></div></div><div style="background-color:lightpink;display:none;" id="updateMealDiv">    <table>        <tr>            <th>Name</th>            <th>Comments</th>            <th>Picture</th>        </tr>        <tr>            <td><input type="text" id="txtName" name="txtName" /></td>            <td><input type="text" id="txtComment" name="txtComment" /></td>        </tr>    </table>    <input type="button" value="update meal" id="btnUpdateMeal" name="btnUpdateMeal"></div><script type="text/javascript">    $("#btnAjax").click(function () {        $.ajax({            url: ‘/AjaxDemo/GetMeal‘,            contentType: ‘application/html; charset=utf-8‘,            type: ‘Get‘,            datatype: ‘html‘        })        .success(function (data) {            $("#meals").html(data);            $("#updateMealDiv").show().appendTo($("#meals"));        })        .error(function (xhr, status, errorThrown) {            alert(errorThrown);        })    });    $("#btnUpdateMeal").click(function () {        var nameVal = $("#txtName").val();        var commentVal = $("#txtComment").val();        $.ajax({            url: ‘/AjaxDemo/GetJsonString‘,            data: JSON.stringify({ name: nameVal, comment: commentVal }),            type: ‘Post‘,            contentType: "application/json; charset=utf-8"        })        .success(function (data) {            var resultStr = "<ul><li>" + data.newName + "</li><li>" + data.newComment + "</li></ul>";            $("#jsonResult").html(resultStr);        })        .error(function (xhr, status, errorMsg) {            alert(errorMsg);        })    });</script>
View Code

三、Controller 處理
使用Json.stringify()來處理json字串

 public class AjaxDemoController : Controller    {        private Db dbContext = new Db();        //        // GET: /AjaxDemo/        public ActionResult Index()        {            return View();        }        public ActionResult GetMeal()        {            Meal[] meals = dbContext.Meals.Where(m => m.Id <= 5).ToArray();            return PartialView("GetMeals", meals);        }        public ActionResult GetJsonString(string name, string comment)        {            return Json(new { newName = name + "Luxuan", newComment = comment + "LLLLL" });        }        public ActionResult UpdateMeal(string mName, string comment)        {            IList<Meal> mealList = dbContext.Meals.ToList();            Meal updatedMeal = mealList.Where(m => m.Name == mName).Single();            updatedMeal.Name = mName;            updatedMeal.Comments = comment;            dbContext.SaveChanges();            mealList = mealList.Where(m => m.Id <= 5).ToArray();            return PartialView("GetMeals", mealList);        }    }
View Code

四、運行結果

 

Ajax retrieve JSON data and Html data from MVC Controllers in ASP.NET MVC

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.