我遇到了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: "<Пред",
nextText: "След>",
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值?
嗨,Anadi。任務是在瀏覽器中顯示帶有樣式文本的完整視圖,以及帶有帶有ReturnedDescription值的TextBoxFor。 –
在這種情況下,您的$ .ajax將不起作用。你必須使用Ajax.Beginform。使用'AddNewInfo.cshtml'更新您的問題,以便我可以更新我的答案。 – Anadi
所以,我會更新主題。 –