在已键入的Razor视图中将ID呈现为Null [英] ID Being Rendered As Null In A Typed Razor View

查看:109
本文介绍了在已键入的Razor视图中将ID呈现为Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的很奇怪,由于某种原因,ViewModel的ID在视图内部被呈现为null.我调试了该应用程序,并检查了视图模型是否已正确映射(使用AutoMapper),并且一切都很好.但是,当我执行@ Model.Id时,它呈现为零,并且当我使用jQuery提醒值时,我得到空".那是怎么回事?

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:在旁注中,您如何告诉该表格不需要过帐且已得到妥善处理?我只是不记得了...绝对不是return false;

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;

推荐答案

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

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() }

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

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

这篇关于在已键入的Razor视图中将ID呈现为Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆