[英]How to send model binded to view from ajax call on button click to controller action?
[英]How to pass whole model object from view to controller action from jquery ajax call on pressing submit button?
我已經這樣嘗試過:
Ajax呼叫:
<script>
$("#Create").click(function () {
var project=@Model;
$.ajax({
url: "/Projects/TempCreateProject",
datatype: "text",
type: "POST",
data: JSON.stringify(project),
success: function (data) {
alert(data.message);
},
error: function () {
// $("#testarea").html("ERROR");
alert("Error");
}
});
});
</script>
動作方式 :
[HttpPost]
public ActionResult TempCreateProject(Project Project)
{
try
{
var skill = _projectService.GetSkillSetById(Project.skillSetId);
Project.SkillSets.Add(skill);
var projectId = _projectService.AddProject(Project);
if (projectId >= 0)
{
var AlertMessage = Project.ProjectName + " Having ProjectId: " + projectId + " Created Successfully.";
return Json(new { success = true, message = "This is a message" });
}
return Json(new { success = false, message = "Project Creation Failed!!!" });
// return RedirectToAction("GetProjectList");
}
catch
{
return View();
}
}
但這甚至沒有調用動作方法。
您可以按照以下步驟進行操作-
// read values (example)
var data1 = $('#txt1').val();
var data2 = $('#txt2').val();
// Create your model
var modelData = { 'Name': data1, 'Code': data2 }; // Name and Code are the model properties
$("#Create").click(function () {
$.ajax({
url: "/Projects/TempCreateProject",
datatype: "json",
type: "POST",
data: JSON.stringify({ Project: modelData }),
contentType: "application/json; charset = utf-8"
success: function (data) {
alert(data.message);
},
error: function () {
// $("#testarea").html("ERROR");
alert("Error");
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.