2011-12-29 258 views
1

我是全新的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(); 

      } 
     } 
    } 

} 

回答

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不起作用。 – 2011-12-29 20:43:48

+1

很酷,如果有意義的話,操作鏈接並沒有真正提交表單。如果你看看生成頁面,你現在注意到表單看起來像

Joe 2011-12-29 20:50:35

+0

感謝Joe,我不知道如果指定了操作鏈接,我仍然需要表單操作。 – 2012-01-04 20:54:17

相關問題