ViewModel의 ID가 어떤 이유로 뷰 내부에서 null로 렌더링되고있는 것은 정말 이상합니다. 응용 프로그램을 디버깅하고 뷰 모델이 실제로 AutoMapper를 사용하여 올바르게 매핑되고 모든 것이 정상인지 확인했는지 확인합니다. 그러나 @ Model.Id를 수행하면 0으로 렌더링되고 jQuery를 사용하여 값을 경고 할 때 "null"이됩니다. 무슨 일 이니?입력 된 면도기 뷰에서 Null로 렌더링되는 ID
<div id="commentBox">
@using (Html.BeginForm("Comment", "Item", new { itemId = Model.Id }))
{
<span class="greenText">answer</span>
<span id="hiddenForId">@this.Hidden(x => x.Id).Id("idPlaceHolder")</span>
<span id="commentInputBox">@this.TextBox(x=>x.CommentText).Id("commentField")</span>
<span id="commentButton">@this.SubmitButton("").Id("commentSubmit").Class("okButton")</span>
}
</div>
<div style="clear:both"></div>
</div>
</div>
<script type="text/javascript">
$(function() {
alert($("idPlaceHolder").html());
$("#commentSubmit").click(function() {
var dataObj = { 'commentText': $("#commentField").val(), 'itemId': $("idPlaceHolder").html() }
$.ajax({
url: '/Item/Comment',
data: dataObj,
type: 'POST',
success: function (result) {
if (result.redirectTo != null && result.redirectTo != '') {
window.location.href = result.redirectTo;
} else {
alert(result.error);
}
}
});
return false;
});
});
</script>
P .: 사이드 노트에 양식을 게시 할 필요가없고 돌봐 주었다고 어떻게 설명합니까? 나는 그것을 기억할 수 없다 ... 확실히 다른 것일 것이다. return false;
false를 반환하는 대신 e.preventDefault()를 사용해야합니다. –
@Robin Clowers : 아! 이것은 정확히 내가 찾고 있던 방법 이었지만 그것이 무엇인지를 기억할 수 없었습니다. Robin, +1에 감사드립니다. :) – Kassem