ID 在类型化 Razor 视图中呈现为 Null

发布于 2024-11-02 11:17:14 字数 1743 浏览 2 评论 0原文

这真的很奇怪,由于某种原因,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情别恋 2024-11-09 11:17:14

部分答案:如果您在表单的 onsubmit 事件上返回 false,它将阻止表单提交。

Partial answer: if you return false on a form's onsubmit event it will prevent the form from submitting.

白云不回头 2024-11-09 11:17:14

在显示视图之前 - 模型确实有 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.

一笔一画续写前缘 2024-11-09 11:17:14

好吧,我终于弄清楚出了什么问题:

$(function () {
    alert($("#idPlaceHolder").val());
    $("#commentSubmit").click(function () {
        var dataObj = { 'commentText': $("#commentField").val(), 'itemId': $("#idPlaceHolder").val() }

我使用的是 $("idPlaceHolder").html() 而不是 $("#idPlaceHolder").val()...语法错误!

Ok I've finally figured out what was going wrong:

$(function () {
    alert($("#idPlaceHolder").val());
    $("#commentSubmit").click(function () {
        var dataObj = { 'commentText': $("#commentField").val(), 'itemId': $("#idPlaceHolder").val() }

I was using $("idPlaceHolder").html() instead of $("#idPlaceHolder").val()... syntax error!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文