简体   繁体   English

如何在ASP.NET MVC中将表单提交到数据库中

[英]How do I submit a form into a database in asp.net MVC

I am building a comment system in asp.net MVC. 我正在asp.net MVC中构建评论系统。 Nothing fancy, but still a little bit above my skill level. 没什么花哨的,但仍然比我的技能水平高一点。 I am using a partialview to display the comment form on the page, and need to have logged in users be able to comment and have the partial view reload what they have submitted. 我正在使用partialview在页面上显示评论表单,并且需要已登录的用户能够发表评论,并使partial视图重新加载他们提交的内容。

My current code is as follows: 我当前的代码如下:

View: 视图:

@using Microsoft.AspNet.Identity
@model DCH.Web.Models.CollaborativeProjectDetailsViewModel
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>
tinymce.init({ selector: '#commentBox',
    plugins: [
        ["advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker"],
        ["searchreplace wordcount visualblocks visualchars code insertdatetime media nonbreaking"],
        ["save table contextmenu directionality emoticons template paste"]
    ],
    statusbar: false,
    menubar: false
});
</script>
@{
var loggedInUser = User.Identity.GetUserName();
}
    <!--Comments-->

<div>
<h3>Comments</h3>
<p> You are logged in as: <span class="commentUser">@loggedInUser</span> 
@Html.ActionLink("(Log Out)", "LogOut", "Account")</p>
</div>
@using (Ajax.BeginForm("CommentForm", null, new AjaxOptions { HttpMethod = "POST",     InsertionMode = InsertionMode.Replace }, new { id = "CollaborativeCommentForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
    @Html.ValidationMessageFor(m => m.Content)
    @Html.TextAreaFor(m => m.Content, new {style = "max-width: 1000px; width = 1000px", @class = "form-control", rows = "10", id = "commentBox"})
</div>
<div class="pull-right comment-submit-top-pad">
    <input type="button" value="Cancel" class="btn btn-danger" />
    <input type="button" value="Submit" class="btn btn-primary" />
</div>

<!--End Comments-->

}

Model: 模型:

[Required]
    public int Id { get; set; }
    public string PostedBy { get; set; }
    public DateTime PostedOn { get; set; }
    [Required(ErrorMessage = "Please fill in a comment below")]
    [AllowHtml]
    public string Content { get; set; }

Controller: 控制器:

[HttpPost]
    public ActionResult CommentForm(CollaborativeProjectDetailsViewModel model)
    {
        if (ModelState.IsValid)
        {
            Session["CollaborativeComments"] = model;

        }
        return PartialView("CollaborativeComments");
    }

Now I went through the tutorials on asp.net and still am stuck on what to do. 现在,我浏览了asp.net上的教程,但仍然坚持该做什么。 I know that I have to be able to db.Comments.Add but am unsure how to generate such code. 我知道我必须能够db.Comments.Add,但是不确定如何生成这样的代码。 Thank you in advance for any help. 预先感谢您的任何帮助。

I would suggest going to http://www.entityframework.org/entity-framework-getting-started/ and doing that walkthrough so you understand the basics of what EF is trying to do. 我建议去http://www.entityframework.org/entity-framework-getting-started/并进行演练,以便您了解EF试图做的基本工作。

Once you have done that, you will know what to do above. 完成此操作后,您将知道上面的操作。 With EF it is really extremely easy, but if you are questioning how the article will show you. 使用EF,这确实非常容易,但是如果您对本文的显示方式有所疑问,请与我们联系。

Good luck. 祝好运。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM