asp.net-mvc 无法弄清楚为什么模型在回发时为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8026868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't figure out why model is null on postback?
提问by NickK
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.
我是 ASP.NET MVC 的新手,我正在尝试创建一个非常简单的博客类型网站,作为学习一切工作方式的一种方式。但是,从评论表单发布到空模型时遇到问题,我不知道为什么。
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:
在博客文章页面上,我有一个“添加评论”链接,它调用一些 JQuery 来呈现一个强类型到 CommentModel 的部分视图。链接也传入了博客文章的 ID,部分代码如下:
<%@ 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" />
<% } %>
The CommentsModel is simple and looks like this (I haven't applied any validation or anything yet):
CommentsModel 很简单,看起来像这样(我还没有应用任何验证或任何东西):
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; }
}
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
{
}
}
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.
一切都按预期进行,除了在发布评论表单时,评论模型为空。我不明白为什么这是空的。当我查看呈现的部分视图的来源时,我可以看到“Post.ID”填充了正确的 ID,但是在提交表单时会丢失。
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.
我在这里遗漏了一些明显的东西吗?我过去设置过与此类似的表单,并且效果很好,我不明白为什么现在不行。提前致谢。
Later Edit:
后来编辑:
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.
我已经输入了错误的代码,改变了public ActionResult CommentForm(CommentModel model)从public ActionResult CommentForm(CommentModel comment)这是造成问题的原因。
Thanks for the help.
谢谢您的帮助。
回答by Pawan Mishra
Similar kind of question has been answered yesterday. Check out : MVC3 - Insert using ViewModel - Object reference not set to an instance of an object
昨天已经回答了类似的问题。签出:MVC3 - 使用 ViewModel 插入 - 未将对象引用设置为对象的实例
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.
我可以看到的问题是,当表单发布时,Post.ID 和 CommentID 被传递,而您的操作需要一个完整的“CommentModel”类型的对象。模型绑定器无法将发布数据映射到相应的模型对象中。
回答by Jeremy Holovacs
Add:
添加:
public int PostID {get; set;}
...to your model, and populate that in your controller as a hidden input. The Post object is not going to parse easily.
...到您的模型,并将其作为隐藏输入填充到您的控制器中。Post 对象不会轻易解析。

