Asp.net mvc 将模型对象数据从视图传递到控制器,再传递到模型?

Asp.net mvc 将模型对象数据从视图传递到控制器,再传递到模型?,asp.net-mvc,razor,Asp.net Mvc,Razor,我是ASP.NETMVC的新手(大约一周),所以仍然有相当多的困惑 如何将当前视图模型传递到控制器中,以便获取模型数据 看法 和一个模型 public class BlogPost { public int Id { get; set; } [Required] [Display(Name="Title of Blog Post")] public string Title { get; set; } public DateTime DateCreat

我是ASP.NETMVC的新手(大约一周),所以仍然有相当多的困惑

如何将当前视图模型传递到控制器中,以便获取模型数据

看法

和一个模型

public class BlogPost
{
    public int Id { get; set; }

    [Required]
    [Display(Name="Title of Blog Post")]
    public string Title { get; set; }

    public DateTime DateCreated { get; set; }

    [Required]
    [Display(Name = "Short Description")]
    public string ShortDescription { get; set; }

    public string LongDescription { get; set; }

    public int HeaderImage { get; set; }

    public ICollection<BlogPost> GetAllPosts()
    {
        ICollection<BlogPost> posts = new List<BlogPost>();

        using (SqlConnection connection = new   SqlConnection(ConfigurationManager.ConnectionStrings["KineticBombardment"].ToString()))
        {
            using (SqlCommand cmd = new SqlCommand("select title, datecreated, shortdescription from blogentries where id > 0", connection))
            {
                cmd.Parameters.Clear();
                connection.Open();
                cmd.ExecuteNonQuery();

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while(reader.Read())
                    {
                        this.ShortDescription = reader["shortdescription"].ToString();
                        this.Title = reader["title"].ToString();
                        this.DateCreated = Convert.ToDateTime(reader["datecreated"].ToString());
                        posts.Add(this);
                    }
                }

                return posts;
            }
        }
    }

    public void CreatePost()
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["KineticBombardment"].ToString()))
        {
            using (SqlCommand cmd = new SqlCommand("insert into blogentries (shortdescription, datecreated, blogtype, title) values (@shortdescription, @datecreated, @blogtype, @title)", connection))
            {
                cmd.Parameters.Clear();
                connection.Open();

                cmd.Parameters.Add("@shortdescription", SqlDbType.VarChar, 500).Value = this.ShortDescription;
                cmd.Parameters.Add("@datecreated", SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@blogtype", SqlDbType.Int).Value = 1;
                cmd.Parameters.Add("@title", SqlDbType.VarChar, 255).Value = this.Title;

                cmd.ExecuteNonQuery();

            }
        }
    }

}
公共类BlogPost
{
公共int Id{get;set;}
[必需]
[显示(Name=“博客文章标题”)]
公共字符串标题{get;set;}
public DateTime DateCreated{get;set;}
[必需]
[显示(名称=“简短描述”)]
公共字符串ShortDescription{get;set;}
公共字符串LongDescription{get;set;}
public int HeaderImage{get;set;}
公共ICollection GetAllPosts()
{
ICollection posts=新列表();
使用(SqlConnection连接=新的SqlConnection(ConfigurationManager.ConnectionString[“KineticBombarding”].ToString())
{
使用(SqlCommand cmd=newsqlcommand(“从id>0的博客条目中选择标题、创建日期、简短描述”,连接))
{
cmd.Parameters.Clear();
connection.Open();
cmd.ExecuteNonQuery();
使用(SqlDataReader=cmd.ExecuteReader())
{
while(reader.Read())
{
this.ShortDescription=reader[“ShortDescription”].ToString();
this.Title=reader[“Title”].ToString();
this.DateCreated=Convert.ToDateTime(读卡器[“DateCreated”].ToString());
增加(本);
}
}
返回岗位;
}
}
}
public void CreatePost()
{
使用(SqlConnection连接=新的SqlConnection(ConfigurationManager.ConnectionString[“KineticBombarding”].ToString())
{
使用(SqlCommand cmd=new-SqlCommand(“插入到blogoentries(shortdescription,datecreated,blogtype,title))值(@shortdescription,@datecreated,@blogtype,@title)”,连接)
{
cmd.Parameters.Clear();
connection.Open();
cmd.Parameters.Add(“@shortdescription”,SqlDbType.VarChar,500)。Value=this.shortdescription;
Add(“@datecreated”,SqlDbType.DateTime).Value=DateTime.Now;
cmd.Parameters.Add(“@blogtype”,SqlDbType.Int).Value=1;
cmd.Parameters.Add(“@title”,SqlDbType.VarChar,255).Value=this.title;
cmd.ExecuteNonQuery();
}
}
}
}
更改:

    <div class="button">
        @Html.ActionLink("Submit", "CreateNewBlogEntry", "Admin" );
    </div>


我做了一些假设,但这应该行得通

我不完全确定为什么,但将控制器操作和控制器添加到Html.BeginForm()并添加一个简单的提交输入就成功了@使用(Html.beginnform(“CreateNewBlogEntry”、“Admin”)),不完全确定ActionLink为什么不能工作。很酷,如果有意义的话,ActionLink并没有真正提交表单。如果查看生成页面,您现在注意到表单看起来像
谢谢Joe,我不知道如果指定了操作链接,我仍然需要表单操作。
public class BlogPost
{
    public int Id { get; set; }

    [Required]
    [Display(Name="Title of Blog Post")]
    public string Title { get; set; }

    public DateTime DateCreated { get; set; }

    [Required]
    [Display(Name = "Short Description")]
    public string ShortDescription { get; set; }

    public string LongDescription { get; set; }

    public int HeaderImage { get; set; }

    public ICollection<BlogPost> GetAllPosts()
    {
        ICollection<BlogPost> posts = new List<BlogPost>();

        using (SqlConnection connection = new   SqlConnection(ConfigurationManager.ConnectionStrings["KineticBombardment"].ToString()))
        {
            using (SqlCommand cmd = new SqlCommand("select title, datecreated, shortdescription from blogentries where id > 0", connection))
            {
                cmd.Parameters.Clear();
                connection.Open();
                cmd.ExecuteNonQuery();

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while(reader.Read())
                    {
                        this.ShortDescription = reader["shortdescription"].ToString();
                        this.Title = reader["title"].ToString();
                        this.DateCreated = Convert.ToDateTime(reader["datecreated"].ToString());
                        posts.Add(this);
                    }
                }

                return posts;
            }
        }
    }

    public void CreatePost()
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["KineticBombardment"].ToString()))
        {
            using (SqlCommand cmd = new SqlCommand("insert into blogentries (shortdescription, datecreated, blogtype, title) values (@shortdescription, @datecreated, @blogtype, @title)", connection))
            {
                cmd.Parameters.Clear();
                connection.Open();

                cmd.Parameters.Add("@shortdescription", SqlDbType.VarChar, 500).Value = this.ShortDescription;
                cmd.Parameters.Add("@datecreated", SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@blogtype", SqlDbType.Int).Value = 1;
                cmd.Parameters.Add("@title", SqlDbType.VarChar, 255).Value = this.Title;

                cmd.ExecuteNonQuery();

            }
        }
    }

}
    <div class="button">
        @Html.ActionLink("Submit", "CreateNewBlogEntry", "Admin" );
    </div>
    <input type="submit" class="button" />
public ActionResult CreateNewBlogEntry(Models.Blog.BlogPost currentBlogModel)
{
    if (ModelState.IsValid)
    {
        currentBlogModel.CreatePost();
        return Content("Created!");
    }

    return View();
}
public ActionResult CreateNewBlogEntry()
{
    return View();
}

[HttpPost]
public ActionResult CreateNewBlogEntry(Models.Blog.BlogPost model)
{
    if (ModelState.IsValid)
    {
        currentBlogModel.CreatePost();
        return Content("Created!");
    }
    return View();
}