简体   繁体   中英

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

I am building a comment system in 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.

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. I know that I have to be able to db.Comments.Add but am unsure how to generate such code. 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.

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.

Good luck. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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