我需要在文本框中更改文本时插入在textareafor中输入的文本。我使用jquery更改函数来完成此操作。我从ajax获取控制器命中动作方法但是,视图中的文本不是传递给模特。 (使用的代码:MVC4 / entity dbfirst / razor ) 以下是我的详细代码。
控制器:
public ActionResult Index()
{
return View();
}
public ActionResult Dailynotes()
{
return View();
}
//I’m getting this code hit from ajaxcall, but dashboard model is null????????
tblDailyNotes note = new tblDailyNotes();
[HttpPost]
public ActionResult Dailynotes(DashboardViewModel model)
{
int noteid = 0;
model.Dailynotes = note.Note;
_scheduler.InsertDailynote(note);//this is the sp to insert the note
return View();
}
Index.cshtml:
<div id="content">
@(Html.Kendo().TabStrip()
.Name("tabstrip")
.Items(tabstrip =>
{
tabstrip.Add().Text("Daily Notes")
.Selected(true)
.LoadContentFrom("Dailynotes", "Scheduler");
})
)
</div>
<div>
@{Html.RenderAction("Dailynotes", "Scheduler");}
</div>
Dailynotes.cshtml:
@model proj.Models.DashboardViewModel
@Html.TextAreaFor(model => model.Dailynotes, new {@class="k-textbox",id="dailynotes"})
<script>
$("#dailynotes").change(function () {
alert("Changed");
debugger;
$.ajax({
cache: true,
type: "POST",
url: window.location.pathname + "Scheduler/Dailynotes",
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
},
complete: function () { }
});
});
Dashboardviewmodel.cs
public class DashboardViewModel
{
public string Dailynotes { get; set; }
public string Weeklynotes { get; set; }
}
我得到ajax调用post动作,但dashboardmodel不会返回我在模型中的文本框中输入的文本。
请帮助..
编辑1: /不工作
$("#dailynotes").change(function () {
alert("Changed");
var dailynotes = $('#dailynotes').val();
debugger;
$.ajax({
url: window.location.pathname + 'Scheduler/Dailynotes',
type: 'POST',
//data: $('form').serialize(),
data:{ model:dailynotes }
})
.success(function (result) {
alert("note saved successfully");
});
});
答案 0 :(得分:1)
您缺少ajax函数的数据参数,您希望在其中发布您的值
http://api.jquery.com/jQuery.ajax/
样品:
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
答案 1 :(得分:1)
怎么样:
$("#dailynotes").change(function () {
alert("Changed");
var dailynotes = $('#dailynotes').val();
debugger;
$.ajax({
url: window.location.pathname + '/Scheduler/Dailynotes',
type: 'POST',
//data: $('form').serialize(),
data:{ model:dailynotes }
})
.success(function (result) {
alert("note saved successfully");
});
});
请注意我已更改了行data:{ model:dailynotes }
。这是因为控制器操作需要一个名为model
即。 public ActionResult Dailynotes(DashboardViewModel model)
我还将类型更改为POST
,因为操作已标记为此属性。
答案 2 :(得分:0)
如果要使用ajax调用将视图上的模型传递给控制器,可以使用
data: $('form').serialize()
如果你只想传递文本,那么只需要传递一个带有类型字符串的参数而不是整个模型。
第二个选项,您可以使用
var dailynotes=$('#dailynotes').val();
并在ajax调用中
data:{DailyNotes:dailynotes};
和控制器操作,接受参数类型为string DailyNotes