想不通为什么模型是空的回发? [英] Can't figure out why model is null on postback?

查看:126
本文介绍了想不通为什么模型是空的回发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的ASP.NET MVC和我试图创建一个非常简单的博客类网站作为学习如何一切正常的手段。但是,我从评论的形式张贴到一个模型,它是空的时候,我不知道为什么有问题。

I'm new to ASP.NET MVC and I'm trying to create a very simple blog type site as a means of learning how everything works. But, I'm having a problem when posting from a comment form to a model which is null and I can't tell why.

在一篇博客文章页面,我有一个添加评论链接,调用一些jQuery来呈现强类型的CommentModel的局部视图。链接通过在博客帖子的ID以及和部分为codeD这样的:

On a blog post page, I have an "add comment" link which calls some JQuery to render a partial view that is strongly typed to the CommentModel. The link passes in the ID of the blog post as well and the partial is coded like:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Blog.Models.CommentModel>" %>

<% using (Html.BeginForm())
   { %>

    <%: Html.HiddenFor(x => x.Post.ID) %>
    <%: Html.HiddenFor(x => x.CommentID) %>

    <%: Html.TextBoxFor(x => x.Name) %><br />
    <%: Html.TextBoxFor(x => x.Email) %><br />
    <%: Html.TextBoxFor(x => x.Website) %><br />
    <%: Html.TextAreaFor(x => x.Comment) %><br />

    <input type="submit" value="submit" />

<% } %>

该CommentsModel简单,看起来像这样(我还没有应用任何验证或任何东西):

The CommentsModel is simple and looks like this (I haven't applied any validation or anything yet):

public class CommentModel
    {
        public BlogPost Post { get; set; }
        public int CommentID { get; set; }

        public string Name { get; set; }
        public string Email { get; set; }
        public string Website { get; set; }
        public string Comment { get; set; }
    }

这是转念一想张贴到一个简单的控制器动作,将添加到数据库中的注释,并返回到该页面。为了简便起见,我已经剥离了大部分的code,但它看起来类似:

This is then supposed to post to a simple controller action which will add the comment to the database and return the user to the page. For the sake of simplicity, I've stripped out most of the code but it looks similar to:

[HttpPost]
        public ActionResult CommentForm(CommentModel model)
        {
            if (ModelState.IsValid)
            {

            }
            else
            {

            }
        }

一切张贴评论表单时正常工作,不同之处在于注释型号为空。我想不通这是为什么空。当我查看渲染局部视图的来源,我可以看到Post.ID填充了正确的ID,但提交表单时就全没了。

Everything works as expected, except that when posting the comment form, the comment model is null. I can't figure out why this is null. When I view the source of the rendered partial view, I can see that the "Post.ID" is populated with the correct ID, but this is lost when the form is submitted.

我在这里丢失了一些东西明显?我已经设置了以往类似这样的形式和它的工作很好,我不能,为什么它不是现在明白了。先谢谢了。

Am I missing something obvious here? I've set up forms similar to this in the past and it's worked fine, I can't understand why its not now. Thanks in advance.

后来编辑:

我已经输入了code不正确,改变了公众的ActionResult CommentForm(CommentModel模型)公众的ActionResult CommentForm(CommentModel评论)这是造成问题的原因。

I had typed the code incorrectly and changed the public ActionResult CommentForm(CommentModel model) from public ActionResult CommentForm(CommentModel comment) which was causing the problem.

感谢您的帮助。

推荐答案

类似的各种问题已经回答了昨天。退房:<一href=\"http://stackoverflow.com/questions/8020474/mvc3-insert-using-viewmodel-object-reference-not-set-to-an-instance-of-an-ob/8020507#8020507\">MVC3 - 插入使用视图模型 - 对象引用不设置到对象的实例

Similar kind of question has been answered yesterday. Check out : MVC3 - Insert using ViewModel - Object reference not set to an instance of an object

我能看到的问题是,当表单发布时,Post.ID和CommentID传递,而你的行动期望类型的完全成熟的对象CommentModel。模型绑定无法post数据映射,到相应的模型对象。

The problem I can see is , when the form is posted, the Post.ID and CommentID are passed, whereas your action is expecting a full blown object of type "CommentModel". The model binder is unable to map the post data, into the corresponding model object.

这篇关于想不通为什么模型是空的回发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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