현재 내가 할 수있는 ASP.NET에서 AJAX를 사용하여 모델을 생성하지만 않는 나는 settingProfile JS 객체에 싸여 데이터를 전송하는 AJAX를 사용하려고하면 그것은 HTTP 500 내부 서버 오류 오류를 반환, 나는 데이터 유형에 문제가 있다고 생각합니다. AJAX에서 Create 메서드를 호출하는 올바른 방법은 무엇입니까?어떻게</p> <p>을 (SettingProfile를 이동하여/만들기) 정기적으로 모델을 만들 수 MVC 5
내 코드 :
모델 :
public class SettingProfile
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Name { get; set; }
public long? UserId { get; set; }
public string Url { get; set; }
}
보기 (JS) :
function saveSettingProfile() {
var name = prompt("Please enter profile name", "");
var url = $("form").serialize(); // Not AJAX url, its a variable in model
var settingProfile = {
Name: name,
Url: url
};
jQuery.ajax({
url: "@Url.Action("Create", "SettingProfile")",
contentType: "application/json; charset=utf-8",
dataType: "json",
method: "POST",
data: settingProfile
}).done(function (response) {
alert("Profile saved successfully");
}).fail(function() {
alert("Could not save profile");
});
}
컨트롤러
: [HttpPost]
//[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Name,Url")] SettingProfile settingProfile)
{
settingProfile.UserId = 8; // TODO: get user id from session
if (ModelState.IsValid)
{
db.SettingProfiles.Add(settingProfile);
db.SaveChanges();
return Json(new { success = true });
}
return Json(new { success = false });
}
HTTP 상태가 500이면 예외 메시지는 무엇입니까? – Anton
Ajax.BeginForm 메서드를 사용할 수없는 이유는 무엇입니까? – Xavr