C# 表单未将数据保存到模型
我使用MVC来建立一个博客。我想要的是将post注释保存到数据库中相应的位置,但它不起作用 我的帖子模式如下:C# 表单未将数据保存到模型,c#,entity-framework,asp.net-mvc-4,C#,Entity Framework,Asp.net Mvc 4,我使用MVC来建立一个博客。我想要的是将post注释保存到数据库中相应的位置,但它不起作用 我的帖子模式如下: public class Post { [Key] public int PostId { get; set; } public string Title { get; set; } public DateTime CreatedDate{get;set;} public DateTime UpdateDate { get; set; }
public class Post
{
[Key]
public int PostId { get; set; }
public string Title { get; set; }
public DateTime CreatedDate{get;set;}
public DateTime UpdateDate { get; set; }
public string Body { get; set; }
public ICollection<Comment> Comments { get; set;}
public ICollection<Tag> Tags { get; set; }
}
public class Comment
{
[Key]
public int CommentId { get; set; }
public int PostId { get; set; }
[ForeignKey("PostId")]
public virtual Post Post{get; set;}
public string CommentCreateDate { get; set; }
public string CommentUpdateDate { get; set; }
public string CommeterName { get; set; }
public string EmailAddress { get; set; }
public string CommentText { get; set; }
public bool Approved { get; set; }
}
@using (Html.BeginForm(new {id = Model.CommentId}))
{
<fieldset>
<legend>Enter Comment</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CommeterName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.CommeterName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.EmailAddress)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CommentText)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.CommentText)
</div>
<p>
<input type="submit" value="Create comment" />
</p>
</fieldset>
}
我有以下行动方法:
[HttpGet]
public ActionResult CreateComment()
{
return View();
}
[HttpPost]
[ValidateInput(false)]
public ActionResult CreateComment(int id, string name, string email, string txt, bool aproved = false)
{
Post post = GetPost(id);
Comment comment = new Comment();
comment.Post = post;
comment.CommentCreateDate = DateTime.Now.ToString();
comment.CommeterName = name;
comment.EmailAddress = email;
comment.CommentText = txt;
comment.Approved = aproved;
db.Comments.Add(comment);
db.SaveChanges();
m_commentList.Add(comment);
return RedirectToAction("CreateComment", new { id = id });
}
在我看来,我正在尝试:
@model Blog.Models.Comment
@{
ViewBag.Title = "CreateComment";
}
<h2>Create a Comment</h2>
@using (Html.BeginForm())
{
<fieldset>
<legend>Enter Comment</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CommeterName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.CommeterName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.EmailAddress)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CommentText)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.CommentText)
</div>
<p>
<input type="submit" value="Create comment" />
</p>
</fieldset>
}
为什么会这样?有人能帮忙吗
谢谢你你的行动签名应该是:
public ActionResult CreateComment(Comment model)
为表单字段生成的名称将绑定回同一模型类的属性。例如,框架无法知道CommenterName
属性是否应该与操作的name
参数匹配
您的第二个示例没有什么意义-您试图写出ID,但从未设置ID。事实上,您甚至没有将注释
与表单一起传递给视图,这就是为什么会出现NullReferenceException
:
[HttpGet]
public ActionResult CreateComment()
{
return View();
}
此外,您还应该注意向模型和操作公开哪些字段。例如,用户只需通过浏览器的开发控制台添加以下标记,就可以轻松地强制批准其评论:
<input type="hidden" name="approved" value="true" />
然后将其映射到您的操作中的注释
:
[HttpPost]
public ActionResult CreateComment(CommentViewModel model)
{
var comment = new Comment();
comment.CommenterName = model.Name;
// etc...
}
这会阻止用户设置诸如
Approved
和CreatedDate
之类的内容,不管您需要什么-我只是想为您演示一种更好的模型绑定方法。剩下的就看你了。谢谢你的帮助!!!但是仍然有必要像这样在签名中传递id:public ActionResult CreateComment(int?id,CommentViewModel model)没问题-请按照我回答的第二部分使用适当的视图模型(如果您还没有)。
public class CreateCommentViewModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Text { get; set; }
}
[HttpPost]
public ActionResult CreateComment(CommentViewModel model)
{
var comment = new Comment();
comment.CommenterName = model.Name;
// etc...
}