ASP.Net에 문제가있어 머리가 찢어지기 쉽습니다. 다른 접근 방식을 사용하여 모델의 일부를 바인딩 할 수는 있지만 모든 데이터를 바인딩 할 수있는 방법은 없습니다.복잡한 형식의 ASP.NET MVC 매개 변수 바인딩이 실패했습니다
컨트롤러 액션 서명 JSON 데이터를 전송하도록 시도 ProfileKnockOutQuestionModel 클래스
public sealed class ProfileKnockOutQuestionModel {
/// <summary>
/// Parameterless constructor is required for auto-passing GET/POST data to controller actions.
/// </summary>
public ProfileKnockOutQuestionModel() { }
[Required]
[DisplayName("Lead Type Id")]
public Int32 AdminLeadType { get; set; }
//[Required]
//[DisplayName("Question Type")]
[UIHint("GridForeignKey")]
public Int16 QuestionTypeId { get; set; }
[Required]
[DisplayName("Question Text")]
public String QuestionText { get; set; }
[Required]
[DisplayName("Pass Answer")]
public String Answer1Text { get; set; }
[Required]
[DisplayName("Fail Answer")]
public String Answer2Text { get; set; }
[Required]
[DisplayName("Price")]
[DataType(DataType.Currency)]
[Range(0, Int16.MaxValue)]
public Decimal Price { get; set; }
public Int32 ProfileKnockOutQuestionId { get; private set; }
public Int32 ProfileId { get; private set; }
public Int32 ParentProfileId { get; private set; }
public Int32 CustomerId { get; private set; }
public Int32 AdminKnockOutQuestionId { get; private set; }
관련 데이터 멤버 인코딩
// Post: Profile/{customerId}/SetKnockOutQuestions/{profileId}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SetKnockOutQuestions(Int32 customerId,
Int32 profileId,
Int32 parentProfileId,
IEnumerable<ProfileKnockOutQuestionModel> questions)
{
,
$.ajax({
url: "/mvc/Profiles/78219/SetKnockOutQuestions/1605111",
data: JSON.stringify({ questions: data,
parentProfileId : 1605105
}),
type: "POST",
contentType: "application/json; charset=utf-8"
});
이 데이터를 전송하는 자바 스크립트 코드 (간결함을 위해 손질)
{
"questions":[
{
"AdminKnockOutQuestionId":8,
"AdminLeadType":4,
"Answer1Text":"Ya really!",
"Answer2Text":"no wai",
"CustomerId":78219,
"ParentProfileId":1605105,
"Price":2,
"ProfileId":1605111,
"ProfileKnockOutQuestionId":0,
"QuestionText":"Really? Auto",
"QuestionTypeId":0
},
하지만 작업을 마치면 모델 바인더는 문자열, 가격 및 AdminLeadType 만 바인딩합니다. 나머지 데이터는 모두 0입니다. 모든 올바르게 구문 분석 된 필드가 있었고 아무 것도 변경되지 않았다는 것을 알게 된 후 [Required]
및 [DisplayName("")]
특성을 나머지 숫자 필드에 추가하려고했습니다.
내가 보내려고하는 다른 방법은 일반적인 형식의 인코딩 된 게시물입니다.
는 AXAJ의에 javscript는
크롬의 네트워크 관리자에서 붙여 넣은 다음 (복사/같은 게시물 본문에 결과$.ajax({
url: "/mvc/Profiles/78219/SetKnockOutQuestions/1605111",
data: { questions: data,
parentProfileId : 1605105
},
type: "POST",
});
내가 대신 않은 구문 분석, URL 인코딩 된 버전을 게시 할 수도 있지만 그냥 저를 신뢰 앰퍼샌드 등은 모두 순서대로 사용됩니다) :
questions[0][AdminKnockOutQuestionId] = 8
questions[0][AdminLeadType] = 4
questions[0][Answer1Text] = Ya really!
questions[0][Answer2Text] = no wai
questions[0][CustomerId] = 78219
questions[0][ParentProfileId] = 1605105
questions[0][Price] = 2
questions[0][ProfileId] = 1605111
questions[0][ProfileKnockOutQuestionId] = 0
questions[0][QuestionText] = Really? Auto
questions[0][QuestionTypeId] = 0
이것은 반대의 문제가 있습니다. 모델 상태가 괴롭히고 Request 객체의 VS 디버거 뷰에서 명확하게 볼 수는 있지만 모든 String 값이 누락되었다고해서 아무 것도 바인딩되지 않습니다.
내 모델 바인딩에 문제가 있습니까?
나는 지금 당장 자신에게 좌절감을 느낀다. 내가 12 번 간과 한 것을 붙잡아 주셔서 감사합니다. –
기꺼이 도와 드리겠습니다. –