2016-12-25 67 views
1

我遇到了ajax問題。 這是代碼。asp.net mvc。如何通過ajax視圖返回修改

型號

public class ViewModel 
{ 
    public long requestedVar { get; set; } 
    public string ReturnedDescription { get; set; } 
} 

查看 名稱:AddNewInfo

<head> 
    <script> 

    $.datepicker.regional['ru'] = { 
      closeText: "Закрыть", 
      prevText: "&#x3C;Пред", 
      nextText: "След&#x3E;", 
      currentText: "Сегодня", 
      monthNames: [ 
       "Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", 
       "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь" 
      ], 
      monthNamesShort: [ 
       "Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", 
       "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь" 
      ], 
      dayNames: ["воскресенье", "понедельник", "вторник", "среда", "четверг", "пятница", "суббота"], 
      dayNamesShort: ["вск", "пнд", "втр", "срд", "чтв", "птн", "сбт"], 
      dayNamesMin: ["Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"], 
      weekHeader: "Нед", 
      dateFormat: "dd.mm.yy", 
      firstDay: 1, 
      isRTL: false, 
      showMonthAfterYear: false, 
      yearSuffix: "" 
     }; 
    </script> 
    <script> 
    var GlobalVar1=0; 

    $("#dropdownlist1").change(function getID() { 
    <some code> 
    GlobalVar1 = selectedValue; 
    alert(GlobalVar1); 

    $.ajax({ 
     url: "@Url.Action("myMethod1", "Home")", 
     type: 'POST', 
     async: false, 
     data: { requestedVar : GlobalVar1}, 
     success: function (data) { 
      alert('OK'); 
      $("html").html(result); 
     }, 
     error: function (error) { 
      alert('error'); 
     } 
    }); 
}); 
    </script> 
    </head> 
    <body> 
    <div> 
     @Html.DropDownListFor(m => m.SelectedDirectionID, new SelectList(Model.DirectionList.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.DirectionName }), "Value", "Text"),"", new { @class = "Duration", required = "required", id="dropdownlist1" }) 
     @Html.ActionLink("+", "_AddDirection", "Home", null, new { @class = "modal-link btn btn-success"}) 
     <div>@Html.DisplayName("Choosen description")</div> 
     <div>@Html.TextBoxFor(m => m.ReturnedDescription, new { @class = "Duration", disabled = "disabled" })</div> 
    </div> 
    </body> 

控制器:

[HttpPost] 
    public System.Web.Mvc.ActionResult myMethod1(ViewModel m) 
    { 
     var db = new DB(); 
     ModelState.Clear(); 

     var returnedVal = db.GetDesctiption(m.requestedVar).First(); 
     m.ReturnedDescription = returnedVal.Description; 

     var vm = DataFactory.Refresh(); 
     vm.ReturnedDescription = m.ReturnedDescription; 

     return View("AddNewInfo", vm); 
    } 

UPDATE

這一切是如何工作的,現在: 阿賈克斯成功地發送了數據的請求後,我的控制器返回查看,在瀏覽器中顯示這個觀點,但返回的視圖中包含任何JS腳本。

問題: 如何完全返回到瀏覽器也可以使用JS腳本查看和ReturnedDescription值?

回答

0

對於通過Ajax和MVC模式恢復數據,您應該從控制器返回一個JSON(例如)類型的數據,以便在通過ajax查看它之後進行恢復。

你必須使用:

return Json(vm, JsonRequestBehavior.AllowGet); 

相反的:

return View("AddNewInfo", vm); 

完成代碼將是:

[HttpPost] 
public JsonResult myMethod1(ViewModel m) 
{ 
    var db = new DB(); 
    ModelState.Clear(); 

    var returnedVal = db.GetDesctiption(m.requestedVar).First(); 
    m.ReturnedDescription = returnedVal.Description; 

    var vm = DataFactory.Refresh(); 
    vm.ReturnedDescription = m.ReturnedDescription; 

    return Json(vm,JsonRequestBehavior.AllowGet); 
} 

約你在哪裏使用該代碼的方法治療。應該是您通過Ajax調用請求數據的地方。

在旁邊,我會以JSON類型從Ajax調用發送數據:

data: JSON.stringify(dataToSend) 

完成的代碼將是(如果您通過JSON類型發送數據時,你會恢復它的字符串類型以及):

<script> 
    var GlobalVar1=0; 

    $("#dropdownlist1").change(function getID() { 
    <some code> 
    GlobalVar1 = selectedValue; 
    alert(GlobalVar1); 
    $(document).ready(function() { 
     $.ajax({ 
      url: "@Url.Action("myMethod1", "Home")", 
      type: 'POST', 
      async: false, 
      success: function (data) { 
       alert("Sending complete"); 
      }, 
      error: function (error) { 
       alert('error'); 
      } 
     }); 
    }); 
}; 
0

步驟我

改變你的操作方法,並返回一個JSON結果

[HttpPost] 
    public JsonResult myMethod1(ViewModel m) 
    { 
     var db = new DB(); 
     ModelState.Clear(); 

     var returnedVal = db.GetDesctiption(m.requestedVar).First(); 
     m.ReturnedDescription = returnedVal.Description; 

     var vm = DataFactory.Refresh(); 
     vm.ReturnedDescription = m.ReturnedDescription; 

     return Json(vm,JsonRequestBehavior.AllowGet); 
    } 

步驟II

更改阿賈克斯成功選項。

$.ajax({ 
     url: "@Url.Action("myMethod1", "Home")", 
     type: 'POST', 
     async: false, 
     data: { requestedVar : GlobalVar1}, 
     success: function (data) { 
      if(data.ReturnedDescription){ 
       $('#ReturnedDescription').val(data.ReturnedDescription); // This line to be noted. 
      } 
      // alert("Sending complete"); 
     }, 
     error: function (error) { 
      alert('error'); 
     } 
    }); 

另外不要忘記保持你的代碼在$(文檔)裏面。準備好

+0

嗨,Anadi。任務是在瀏覽器中顯示帶有樣式文本的完整視圖,以及帶有帶有ReturnedDescription值的TextBoxFor。 –

+0

在這種情況下,您的$ .ajax將不起作用。你必須使用Ajax.Beginform。使用'AddNewInfo.cshtml'更新您的問題,以便我可以更新我的答案。 – Anadi

+0

所以,我會更新主題。 –

相關問題