2011-12-29 12 views
1

私はASP.net MVC(1週間ほど)の新品ですので、まだまだ混乱があります...モデルオブジェクトデータをビューからコントローラにモデルに渡す?

現在のビューモデルをコントローラに渡す方法を教えてくださいモデルデータを取得できますか?私はその後、

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

     return View(); 
    } 

のコントローラとのモデルを持っている

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

      } 
     } 
    } 

} 

答えて

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

に私がなぜ完全にわからないんだけど、私はいくつかの仮定を作ったが、それは

+0

を動作するはずですコントローラーアクションとコントローラーをHtml.BeginForm()に追加し、単純なサブミット入力を追加することがそのトリックでした。 @using(Html.BeginForm( "CreateNewBlogEntry"、 "Admin"))、ActionLinkが動作しない理由を完全にはわかりません。 –

+1

アクションリンクが実際にフォームを送信していなかった場合は意味があります。生成するページを見ると、フォームが '

' – Joe

+0

のようになっていることに気がつきました。ありがとうジョー、アクションリンクが指定されていればフォームアクションが必要でした。 –

関連する問題