将模型对象数据从视图,控制器传递到模型?

时间:2011-12-29 18:56:19

标签: asp.net-mvc razor

我是ASP.net MVC的新手(大约一个星期),所以仍然有很多混乱......

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

查看

@model KineticBomardment.Models.Blog.BlogPost

@{
    ViewBag.Title = "Index";
}


@using (Html.BeginForm())
{
@Html.ValidationSummary(false)

    <fieldset class="block">
       <div class="input">
            @Html.LabelFor(x => x.Title)
            @Html.EditorFor(x => x.Title)
        </div>
         <div class="input">
            @Html.LabelFor(x => x.ShortDescription)
            @Html.EditorFor(x => x.ShortDescription)
        </div>

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

    </fieldset>

}

然后我有一个

的控制器
 public ActionResult CreateNewBlogEntry(Models.Blog.BlogPost currentBlogModel)
    {
        if (ModelState.IsValid)
        {
            currentBlogModel.CreatePost();
            return Content("Created!");
        }

        return View();
    }

的模型
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();

            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

变化:

    <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();
}

我做了一些假设,但这应该有效