尝试使用数据表和JavaScript将帖子数据显示到显示页面上

时间:2019-03-26 14:44:32

标签: javascript c# jquery asp.net entity-framework

因此,我目前正在从事类似论坛的项目,该项目涉及用户能够将帖子提交至数据库,然后将帖子标题显示在数据表上。标题的超链接将根据通过URL提交的postId将用户重定向到postDisplay页面。

该项目是使用Entity Framework的asp.net网络表单。

以下代码是Post.cs类

 public class Post
    {
        //The post ID
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int postId { get; set; }
        // Foreign key to customer
        public string Id { get; set; }
        public string postTitle { get; set; }
        public string postBody { get; set; }
        public string postDepartment { get; set; }
        public string postCategory { get; set; }
        public bool postAnonymous { get; set; }
        public int postLikes { get; set; }
        public int postDislikes { get; set; }
        public DateTime postDate { get; set; } 
    }

下面的代码负责将帖子提交给数据库

protected void AddPost(object sender, EventArgs e)
        {
            ApplicationUserManager _userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
            ApplicationUser user = _userManager.FindByName<ApplicationUser, string>(HttpContext.Current.User.Identity.Name);


            var department = "";
            using (var _dbContext = new ApplicationDbContext())
            {
                department = _dbContext.Departments.FirstOrDefault(c => c.deptId == user.deptId).deptName;
            }


            Post newPost = new Post()
            {
                postTitle = inputTitle.Text,
                postBody = inputBody.Text,
                postCategory = inputCategory.SelectedValue,
                postAnonymous = Convert.ToBoolean(Int32.Parse(inputAnonymous.SelectedValue)),
                Id = user.Id,
                postDepartment = department,
                postDate = DateTime.Now,
            };

            using (var _dbContext = new ApplicationDbContext())
            {
                _dbContext.Posts.Add(newPost);
                _dbContext.SaveChanges();
            }

            //Display success message and clear the form.
            string message = "Your suggestion has been submitted successfully!";
            string script = "window.onload = function(){ alert('";
            script += message;
            script += "');";
            script += "window.location = '";
            script += Request.Url.AbsoluteUri;
            script += "'; }";
            ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
        }

下面的代码负责创建将TableId超链接到postDisplay页面的同时查看帖子标题的DataTable

<div  class="jumbotron">

    <table class="display" id="postsTable">
        <thead>
            <tr>
                <th>Title</th>
                <th>Category</th>
                <th>Date</th>

            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
  <br />

</div>
 <asp:HiddenField id="deptName" runat="server"/>
 <script type="text/javascript">



     $(document).ready(function () {

        $('#postsTable') .DataTable({
            pageLength: "5",
            ajax: {
                url: "/api/posts/GetDatatables",
                type: "GET",

                dataType: "json",
                dataSrc: ""
            },
            columns: [
                {
                    render: function (data, type, row, meta) {

                        return '<a href="/PostDisplay/LoadPost?postId="' + row.postId + '">' + row.postTitle+'</a>';

                    }
                },
                {data: "postCategory"},
                {data: "postDate"},

            ]
        });
    });
</script>

现在对于似乎似乎出错的最后部分,postDisplay页面以

开头
protected void Page_Load(object sender, EventArgs e)
        {

            int postId = int.Parse(Request.QueryString["postId"]);


        }

根据调试器,postId被正确请求并被解析为一个int,但是当该值随后显示为int postId时,它实际上没有在URL中携带数字吗?

<------------------------- EDIT -------------------- ------>

上面的代码行显示以下错误

"Input string was not in a correct format."

任何建议将不胜感激。

0 个答案:

没有答案