代码之家  ›  专栏  ›  技术社区  ›  asfsadf

jQuery发布到ASP.NETMVC2动作,字符串绑定,整数不绑定…?

  •  0
  • asfsadf  · 技术社区  · 15 年前

    以下是JS:

    $('#createReview').click(function () {
        CKEDITOR.instances['ReviewText'].updateElement();
        $.ajax({
            type: 'POST',
            cache: false,
            url: '/Review/Create',
            data: $('#reviewForm').serialize(),
            dataType: 'html',
            success: function (response) {
                $('#bookReview').html(response);
            }
        });
        return false;
    });
    

    “createReview”是

    行动:

        [HttpPost, ExportModelState]
        public ActionResult Create(Review review)
        {
            if (ModelState.IsValid)
            {
                if (review.Create())
                    return PartialView("EditReview", review);
            }
    
            return RedirectToAction("Edit");
        }
    

    最奇怪的是,当我在调试模式下运行它时,

    我在常规模式和调试模式之间来回切换,每次都做同样的事情。

    我不知所措。

    编辑:

    下面是Serialize()调用的完整输出,它不适合注释:

    分数=0&分数=0&分数=0&分数=0&分数=0&分数=0&分数=0&分数=0&分数=0&分数=0&分数=0&Book.Review.Rating=0&评级=0&审阅ID=0&ParentBookID=1&查看文本=%3Cp%3E%0A%09I%26%2339%3Bm+an+idiot%3C%2Fp%3E%0A%3Cbr+%2F%3E%0A%3Cdiv+firebugversion%3D%221.5.4%22+id%3D%22\u firebugConsole%22+style%3D%22显示%3A+none%3B%22%3E%0A%09%26nbsp%3B%3C%2Fdiv%3E%0A%3Cbr+%2F%3E%0A&DateCreated=1%2F1%2F0001+12%3A00%3A00+AM

    编辑#2:

    好吧,所有这些“分数”输入都来自jQuery Raty插件,它很快就被取消了插件。

    Firebug正在破坏来自CKEditor实例的文本,它甚至在表单提交之前都没有得到更新。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Darin Dimitrov    15 年前

    您既没有展示模型类的外观,也没有展示窗体包含哪些输入元素。试图重现这个问题,这是我创建的一个工作示例。它应该非常接近您的场景:

    型号:

    public class Review
    {
        public int ReviewID { get; set; }
        public int ParentBookID { get; set; }
        public int Rating { get; set; }
        public string ReviewText { get; set; }
        public int[] Scores { get; set; }
    }
    

    public class ReviewController : Controller
    {
        public ActionResult Edit()
        {
            var model = new Review
            {
                ReviewID = 1,
                Rating = 5,
                Scores = new[] { 1, 2, 3 }
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Create(Review review)
        {
            if (ModelState.IsValid)
            {
                if (review.Create())
                {
                    return PartialView("EditReview", review);
                }
            }
    
            // Notice that redirecting in an AJAX invoked action will simply
            // send an HTTP redirect to the Edit action and redisplay the whole page
            // which is probably not what you are looking for. Maybe it would be better
            // to return a partial here.
            return RedirectToAction("Edit");        
        }
    }
    

    查看:

    <% using (Html.BeginForm("Create", "Review", FormMethod.Post, new { id = "reviewForm" })) { %>
        <div>
            <%: Html.LabelFor(x => x.ReviewID)%>
            <%: Html.TextBoxFor(x => x.ReviewID)%>  
        </div>
        <div>
            <%: Html.HiddenFor(x => x.ParentBookID)%>  
        </div>
        <div>
            <%: Html.LabelFor(x => x.Rating)%>
            <%: Html.TextBoxFor(x => x.Rating)%>  
        </div>
        <div>
            <%: Html.LabelFor(x => x.ReviewText)%>
            <%: Html.TextAreaFor(x => x.ReviewText)%>  
        </div>
        <%: Html.EditorFor(x => x.Scores)%>
    
        <input type="submit" value="Review" />
    <% } %>
    
    <div id="bookReview"></div>
    

    <script type="text/javascript" src="<%: Url.Content("~/scripts/jquery-1.4.1.min.js") %>"></script>
    <script type="text/javascript">
    $(function () {
        $('#reviewForm').submit(function () {
            $.ajax({
                type: this.method,
                url: this.action,
                data: $(this).serialize(),
                dataType: 'html',
                success: function (response) {
                    $('#bookReview').html(response);
                }
            });
            return false;
        });
    });
    </script>
    

    你也可以看看 jquery form plugin 因此,您的脚本可以简化为:

    $(function () {
        $('#reviewForm').ajaxForm(function(response) {
            $('#bookReview').html(response);
        });
    });
    
        2
  •  0
  •   asfsadf    15 年前