ID 在类型化 Razor 视图中呈现为 Null
这真的很奇怪,由于某种原因,ViewModel 的 Id 在视图中被渲染为 null。我调试了应用程序并检查视图模型实际上是否正确映射(使用 AutoMapper)并且一切正常。但是当我执行 @Model.Id 时,它呈现为零,当我使用 jQuery 警告该值时,我得到“null”。那么到底是怎么回事呢?
<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>
PS:顺便说一句,你如何告诉表格它不需要发布并且它已经被处理了?我只是不记得了......这绝对是除了 return false;
之外的东西
This is really weird, for some reason the Id of the ViewModel is being rendered as null inside the view. I debugged the application and checked that the view model is actually being mapped correctly (using AutoMapper) and everything is fine. But when I do @Model.Id, it is rendering as zero, and when I alert the value using jQuery I get "null". So what's going on?
<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.S: on a side note, how do you tell the form that it doesn't need to post and that it has been taken care of? I just cannot remember it... It's definitely something other than return false;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
部分答案:如果您在表单的 onsubmit 事件上返回 false,它将阻止表单提交。
Partial answer: if you return false on a form's onsubmit event it will prevent the form from submitting.
在显示视图之前 - 模型确实有 ID 值,对吗?在这种情况下,这是在一个帖子上吗?如果是这样 - 如果出现问题并且您在没有重定向的情况下重新显示表单,MVC 会假设出现错误,它将使用发布的值中的内容写回 - 假设您正在创建一条新记录并且评论 id 最初为空。如果这不是在发布/重新显示期间,则此答案不适用。
Right before you display the view - the model does have a value for ID, correct? In that case is this on a post? If so - if there are issues and you redisplay the form without a redirect MVC assumes an error it will use what was in the posted values to write back out- assuming you are creating a new record and the comment id was originally null. if this isnt during post/redisplay then this answer doesn't apply.
好吧,我终于弄清楚出了什么问题:
我使用的是
$("idPlaceHolder").html()
而不是$("#idPlaceHolder").val()...语法错误!
Ok I've finally figured out what was going wrong:
I was using
$("idPlaceHolder").html()
instead of$("#idPlaceHolder").val()
... syntax error!