MVC3绑定到集合数据模型

时间:2011-08-21 13:57:58

标签: asp.net-mvc-3 binding

我的问题不在于显示有关收集数据更改的数据。

我的具体方案是需要允许用户从列表中删除多个项目。我不知道我是否会以一种局部的方式接近它。

列表是私人消息的集合。我的视图模型包含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>

问题是,当我检查几个复选框并点击删除按钮时,我在模型中什么都没有。

同样,我确定我没有以正确的方式接近这一点。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您必须为模型创建一个Controller类。然后创建返回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 = "abc@abc.com", Subject = "Subject 1", Message = "Message 1", DateTimeSent = DateTime.Now, Delete = false });
            myPrivateMessages.Add(new PrivateMessage { From = "abc1@abc.com", Subject = "Subject 2", Message = "Message 2", DateTimeSent = DateTime.Now, Delete = false });
            myPrivateMessages.Add(new PrivateMessage { From = "abc2@abc.com", Subject = "Subject 3", Message = "Message 3", DateTimeSent = DateTime.Now, Delete = false });
            myPrivateMessages.Add(new PrivateMessage { From = "abc3@abc.com", Subject = "Subject 4", Message = "Message 4", DateTimeSent = DateTime.Now, Delete = false });
            myPrivateMessages.Add(new PrivateMessage { From = "abc4@abc.com", Subject = "Subject 5", Message = "Message 5", DateTimeSent = DateTime.Now, Delete = false });

            return myPrivateMessages;
        }        
    }
}

控制器类

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"/>
}

答案 1 :(得分:0)

您需要有一个与Html.BeginForm关联的控制器,其中将发布表单。 例如

 <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.
 }