2011-08-21 119 views
1

我的問題不在於顯示有關收集數據更改的數據。MVC3綁定到收集數據模型

我的具體情況是需要允許用戶從列表中刪除多個項目。我不知道我是否以一種方便的方式接近這一點。

列表是私人消息的集合。我的視圖模型包含To,From,Subject和「刪除」的bool字符串。

public class PrivateMessagesModel { 

    public PrivateMessagesModel() 
    { 
     PrivateMessages = new List<PrivateMessageReceivedModel>(); 
    } 

    public List<PrivateMessageReceivedModel> PrivateMessages; 
} 

public class PrivateMessageReceivedModel 
{ 

    [DataType(DataType.Text)] 
    [Display(Name = "From")] 
    public string From { get; set; } 

    [DataType(DataType.Text)] 
    [Display(Name = "Subject")] 
    public string Subject { get; set; } 

    [DataType(DataType.Text)] 
    [Display(Name = "Message")] 
    public string Message { get; set; } 

    [DataType(DataType.Text)] 
    [Display(Name = "Date")] 
    public DateTime DateTimeSent { get; set; } 

    [DataType(DataType.Text)] 
    [Display(Name = "Delete")] 
    public bool Delete { get; set; } 

} 

要顯示的代碼如下所示。並行得通。

@

model ScaleRailsOnline.Models.PrivateMessagesModel 
@{ 
    ViewBag.Title = "Private Messages"; 
} 
<div id="content"> 
    <div class="content"> 
     <h2> 
      Private Messages</h2> 
     @using (Html.BeginForm()) 
     { <table> 
      @for (int i = 0; i < Model.PrivateMessages.Count; i++) 
      { 
       <tr> 
        <td> 
         @Html.CheckBoxFor(m => m.PrivateMessages[i].Delete) 
        </td> 
        <td> 
         @Html.DisplayTextFor(m => m.PrivateMessages[i].From) 
        </td> 
       </tr> 
      } 
     </table> 
      <p> 
       <input type="submit" value="Delete" /> 
      </p> 
     } 
    </div> 
</div> 

問題是,當我檢查一對夫婦的複選框,並單擊刪除按鈕,我得不到任何回報。在模型中。

再次,我確定我沒有以正確的方式接近這一點。任何幫助,將不勝感激。

+0

在@using(Html.BeginForm())你沒有指定其將被張貼到任何控制器和操作方法的語句。@使用(Html.BeginForm (「Index1」,「Home」,...) –

回答

0

你需要有一個合作與表單將被髮布的Html.BeginForm相關聯的ntroller。 如

<div id="formid"> 
    @using (Html.BeginForm("Index1", "Home", new AjaxOptions { UpdateTargetId = "formid",OnSuccess ="OnSuccess" }, new { id = "TheForm" })) 
    { 
     @for (int i = 0; i < Model.PrivateMessages.Count; i++) 
     { 
      @Html.CheckBoxFor(m => m.PrivateMessages[i].Delete) 
     } 
     <input type="submit" name="name" value="Submit" /> 
    } 
    </div> 

在控制器

public ActionResult Index1(List<PrivateMessageReceivedModel> mod) 
{ 
    //Now in the mod you will get the value of the delete checkbox. 
} 
+0

我正在使用默認窗體。在索引的HTTPPost版本中,表單上的其他數據顯示出來,但不是收集的刪除。無論檢查多少項目,集合都顯示0。 – Cuthahotha

0

您必須爲您的模型創建一個控制器類。然後創建返回ActionResult類型的方法。然後創建新創建的控制器方法的視圖。

這裏是一個示例代碼。

模型類

namespace Mvc3Application1.Models 
{ 
    public class PrivateMessage 
    { 
     public string From { get; set; } 
     public string Subject { get; set; } 
     public string Message { get; set; } 
     public DateTime DateTimeSent { get; set; } 
     public bool Delete { get; set; } 
    } 

    public class PrivateMessageRepository 
    { 
     public List<PrivateMessage> GetPrivateMessages() 
     { 
      List<PrivateMessage> myPrivateMessages=new List<PrivateMessage>(); 

      //add list of messages in the object myPrivateMessages 
      myPrivateMessages.Add(new PrivateMessage { From = "[email protected]", Subject = "Subject 1", Message = "Message 1", DateTimeSent = DateTime.Now, Delete = false }); 
      myPrivateMessages.Add(new PrivateMessage { From = "[email protected]", Subject = "Subject 2", Message = "Message 2", DateTimeSent = DateTime.Now, Delete = false }); 
      myPrivateMessages.Add(new PrivateMessage { From = "[email protected]", Subject = "Subject 3", Message = "Message 3", DateTimeSent = DateTime.Now, Delete = false }); 
      myPrivateMessages.Add(new PrivateMessage { From = "[email protected]", Subject = "Subject 4", Message = "Message 4", DateTimeSent = DateTime.Now, Delete = false }); 
      myPrivateMessages.Add(new PrivateMessage { From = "[email protected]", Subject = "Subject 5", Message = "Message 5", DateTimeSent = DateTime.Now, Delete = false }); 

      return myPrivateMessages; 
     }   
    } 
} 

Controller類

namespace Mvc3Application1.Controllers { 
    public class PrivateMessageController : Controller 
    { 
     // 
     // GET: /PrivateMessage/ 

     // Show all the private messages  
     public ActionResult ListMessages() 
     { 
      return View(new Models.PrivateMessageRepository().GetPrivateMessages()); 
     } 

     //delete the selected messages and return the collection 
     [HttpPost] 
     public ActionResult ListMessages(FormCollection collection) 
     { 
      List<Models.PrivateMessage> messages = new Models.PrivateMessageRepository().GetPrivateMessages();//all messages 
      List<Models.PrivateMessage> cloneMessages = new List<Models.PrivateMessage>();//messages left for deletion 
      int noOfItems = 0;//no of items deleted 
      int currentItem = 0;//current item in collection 'messages' 

      string[] deletedItems = collection[0].Split(',');//return from HTML control collection 

      for (int i = 0; i < deletedItems.Length; i++) 
      { 
       if (bool.Parse(deletedItems[i]) == false)//if checkbox is unchecked 
       { 
        cloneMessages.Add(messages[currentItem]);//copy original message in cloneMessages collection 
       } 
       else //if checkbox is checked 
       { 
        noOfItems++;      
        i++; 
       } 
       currentItem++; 
      } 
      ViewBag.Items = noOfItems + " message(s) deleted."; 
      return View(cloneMessages); 
     } 

    } } 

查看

@model IEnumerable<Mvc3Application1.Models.PrivateMessage> 

@{ 
    ViewBag.Title = "Private Messages"; 
} 

<h2>Private Messages</h2> 
<div style="color:green;font-weight:bold;">@ViewBag.Items</div> 
<br /> 
@using (Html.BeginForm()) 
{ 

    <table> 
     <tr> 
      <th></th> 
      <th> 
       From 
      </th> 
      <th> 
       Subject 
      </th> 
      <th> 
       Message 
      </th> 
      <th> 
       DateTimeSent 
      </th> 

     </tr> 

    @foreach (var item in Model) 
    { 

     <tr> 
      <td> 
       @Html.CheckBoxFor(modelItem => item.Delete) 
      </td> 
      <td> 
       @Html.DisplayTextFor(modelItem => item.From) 
      </td> 
      <td> 
       @Html.DisplayTextFor(modelItem => item.Subject) 
      </td> 
      <td> 
       @Html.DisplayTextFor(modelItem => item.Message) 
      </td> 
      <td> 
       @Html.DisplayTextFor(modelItem => item.DateTimeSent) 
      </td> 

     </tr> 
    } 
    </table> 

    <input type="submit" value="Delete"/> 
} 
+0

很抱歉,您的回答沒有意義,我已經在視圖中顯示了模型和代碼,並且指出所有部分都適用。因此,表明我有一個控制器。我的問題再次是模型中的集合是空的提交。 – Cuthahotha

+0

在控制器類中,您必須創建兩個Action方法,一個用於HttpGet,一個用於HttpPost操作。所以當你按Submit按鈕時你必須調用HttpPost操作方法(這個方法包含數據庫更新邏輯並返回結果)。 –

+0

我已經添加了你的問題的示例代碼。任何問題,請隨時詢問。 –