MVC 3 ModelView passing parameters between view & controller

I've been playing with MVC 3 in a test project and have the following issue.

I have Group & Subscriber entities and those are coupled through a SubscriberGroup table.

Using the DetailView of Group I open a view of SubscriberGroup containing all subscribers. This list has the option to filter.

So far it all works, however when I call the AddToGroup method on the controller it fails. Specifically it goes into the method but doesn't pass the subscriberCheckedModels list. Am I doing something wrong?

View: SubscriberGroup Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<Mail.Models.SubscriberCheckedListViewModel>" %>
…
<h2 class="common-box-title">
        Add Subscribers to Group</h2>
    <p>
        <% using (Html.BeginForm("Index", "SubscriberGroup"))
           { %>
            <input name="filter" id="filter" type="text" />
            <input type="submit" value="Search" />

        <%} %>
    </p>
            <% using (Html.BeginForm("AddToGroup", "SubscriberGroup", Model,FormMethod.Get, null))
           { %>
           <fieldset>
    <div style="display: inline-block; width: 70%; vertical-align: top;">
        <% if (Model.subscribers.Count() != 0)
           { %>
        <table class="hor-minimalist-b">
            <tr>
                <th>
                    Add To Group
                </th>
                <th>
                    Full Name
                </th>
                <th>
                    Email
                </th>
                <th>
                    Customer
                </th>
            </tr>
            <% foreach (var item in Model.subscribers)
               { %>
            <tr>
                <td>
                    <%= Html.CheckBoxFor(modelItem => item.AddToGroup)%>
                </td>
                <td>
                    <%= Html.DisplayFor(modelItem =>  item.subscriber.LastName)%>
                    <%= Html.ActionLink(item.subscriber.FirstName + " " + item.subscriber.LastName, "Details", new { id = item.subscriber.SubscriberID })%>
                </td>
                <td>
                    <%: Html.DisplayFor(modelItem => item.subscriber.Email)%>
                </td>
                <td>
                    <%: Html.DisplayFor(modelItem => item.subscriber.Customer.Company)%>
                    <%= Html.HiddenFor(modelItem => item.subscriber) %>
                </td>
            </tr>
            <% } %>
            <% ViewBag.subscribers = Model.subscribers; %>                                    probeersel
            <%= Html.HiddenFor(model => model.subscribers) %>                                 probeersel
        </table>
        <%} %>
        <%else
           { %>
        <p>
            No subscribers found.</p>
        <%} %>
        <input type="submit" value="Add Subscribers" />
    </div>
    </fieldset>
            <%} %>

Controller: SubscriberGroupController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Mail.Models;

namespace Mail.Controllers
{
    public class SubscriberGroupController : Controller
    {
        private int groupID;
        private MailDBEntities db = new MailDBEntities();
        //
        // GET: /SubscriberGroup/

        public ActionResult Index(int id)
        {
            groupID = id;
            MembershipUser myObject = Membership.GetUser();
            Guid UserID = Guid.Parse(myObject.ProviderUserKey.ToString());
            UserCustomer usercustomer = db.UserCustomers.Single(s => s.UserID == UserID);
            var subscribers = from subscriber in db.Subscribers
                              where (subscriber.CustomerID == usercustomer.CustomerID) | (subscriber.CustomerID == 0)
                              select new SubscriberCheckedModel { subscriber = subscriber, AddToGroup = false };
            SubscriberCheckedListViewModel test = new SubscriberCheckedListViewModel();
            test.subscribers = subscribers;
            return View(test);
        }



        [HttpPost]
        public ActionResult Index(string filter)
        {
            MembershipUser myObject = Membership.GetUser();
            Guid UserID = Guid.Parse(myObject.ProviderUserKey.ToString());
            UserCustomer usercustomer = db.UserCustomers.Single(s => s.UserID == UserID);
            var subscribers2 = from subscriber in db.Subscribers
                               where ((subscriber.FirstName.Contains(filter)|| subscriber.LastName.Contains(filter))
                               && (subscriber.CustomerID == usercustomer.CustomerID || subscriber.CustomerID == 0))
                               select new SubscriberCheckedModel { subscriber = subscriber, AddToGroup = false };
            SubscriberCheckedListViewModel test = new SubscriberCheckedListViewModel();
            test.subscribers = subscribers2.ToList();
            return View(test);
        }

        [HttpPost]
        public ActionResult AddToGroup(SubscriberCheckedListViewModel test)
        {
            //test is null 
            return RedirectToAction("Details", "Group", new { id = groupID });
        }


    }
}

ViewModel: SubscriberGroupModel

using System.Collections.Generic;
using Mail;
namespace Mail.Models
{
    public class SubscriberCheckedModel
    {
        public Subscriber subscriber { get; set; }
        public bool AddToGroup { get; set; }
    }

    public class SubscriberCheckedListViewModel
    {
        public IEnumerable<SubscriberCheckedModel> subscribers { get; set; }
    }
}

Answers


IEnumerable will not binded automatically, we need to modify the foreach loop in the view with follows,

<% for (int i=0;i<Model.subscribers.Count();i++) 
               { %> 
            <tr> 
                <td> 
                    <%= Html.CheckBoxFor(Model => Model.subscribers[i].AddToGroup)%> 
                </td> 
                <td> 
                    <%= Html.DisplayFor(Model =>  Model.subscribers[i].subscriber.LastName)%> 
                    <%= Html.ActionLink(Model.subscribers[i].subscriber.FirstName + " " + Model.subscribers[i].subscriber.LastName, "Details", new { id = Model.subscribers[i].subscriber.SubscriberID })%> 
                </td> 
                <td> 
                    <%: Html.DisplayFor(Model => Model.subscribers[i].subscriber.Email)%> 
                </td> 
                <td> 
                    <%: Html.DisplayFor(Model => Model.subscribers[i].subscriber.Customer.Company)%> 
                    <%= Html.HiddenFor(Model => Model.subscribers[i].subscriber) %> 
                </td> 
            </tr> 
            <% } %>

First Lets modify your model to have Id field

using System.Collections.Generic;
using Mail;
namespace Mail.Models
{
    public class SubscriberCheckedModel
    {
        public Subscriber subscriber { get; set; }
        public bool AddToGroup { get; set; }
    }

    public class SubscriberCheckedListViewModel
    {
        int GroupId{get;set;}
        public IEnumerable<SubscriberCheckedModel> subscribers { get; set; }
    }
}

Now lets modify controller,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;
    using Mail.Models;

    namespace Mail.Controllers
    {
        public class SubscriberGroupController : Controller
        {
            private int groupID;
            private MailDBEntities db = new MailDBEntities();
            //
            // GET: /SubscriberGroup/

            public ActionResult Index(int id)
            {
                groupID = id;
                MembershipUser myObject = Membership.GetUser();
                Guid UserID = Guid.Parse(myObject.ProviderUserKey.ToString());
                UserCustomer usercustomer = db.UserCustomers.Single(s => s.UserID == UserID);
                var subscribers = from subscriber in db.Subscribers
                                  where (subscriber.CustomerID == usercustomer.CustomerID) | (subscriber.CustomerID == 0)
                                  select new SubscriberCheckedModel { subscriber = subscriber, AddToGroup = false };
                SubscriberCheckedListViewModel test = new SubscriberCheckedListViewModel();
                test.subscribers = subscribers;
test.GroupId=groupID;
                return View(test);
            }



            [HttpPost]
            public ActionResult Index(string filter)
            {
                MembershipUser myObject = Membership.GetUser();
                Guid UserID = Guid.Parse(myObject.ProviderUserKey.ToString());
                UserCustomer usercustomer = db.UserCustomers.Single(s => s.UserID == UserID);
                var subscribers2 = from subscriber in db.Subscribers
                                   where ((subscriber.FirstName.Contains(filter)|| subscriber.LastName.Contains(filter))
                                   && (subscriber.CustomerID == usercustomer.CustomerID || subscriber.CustomerID == 0))
                                   select new SubscriberCheckedModel { subscriber = subscriber, AddToGroup = false };
                SubscriberCheckedListViewModel test = new SubscriberCheckedListViewModel();
                test.subscribers = subscribers2.ToList();
                return View(test);
            }

            [HttpPost]
            public ActionResult AddToGroup(SubscriberCheckedListViewModel test)
            {
                var id=test.GroupId; 
                return RedirectToAction("Details", "Group", new { id = groupID });
            }


        }

Now lets modify the view,

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<Mail.Models.SubscriberCheckedListViewModel>" %>
…
<h2 class="common-box-title">
        Add Subscribers to Group</h2>
    <p>
        <% using (Html.BeginForm("Index", "SubscriberGroup"))
           { %>
            <input name="filter" id="filter" type="text" />
            <input type="submit" value="Search" />

        <%} %>
    </p>
            <% using (Html.BeginForm("AddToGroup", "SubscriberGroup", Model,FormMethod.Get, null))
           { %>
           <fieldset>
    <div style="display: inline-block; width: 70%; vertical-align: top;">
        <% if (Model.subscribers.Count() != 0)
           { %>
           <%: Html.HiddenFor(m=>m.GroupId) %>
        <table class="hor-minimalist-b">
            <tr>
                <th>
                    Add To Group
                </th>
                <th>
                    Full Name
                </th>
                <th>
                    Email
                </th>
                <th>
                    Customer
                </th>
            </tr>
            <% for (int i=0;i<Model.subscribers.Count();i++) 
               { %> 
            <tr> 
                <td> 
                    <%= Html.CheckBoxFor(M => M.subscribers[i].AddToGroup)%> 
                </td> 
                <td> 
                    <%= Html.DisplayFor(M =>  M.subscribers[i].subscriber.LastName)%> 
                    <%= Html.ActionLink(Model.subscribers[i].subscriber.FirstName + " " + Model.subscribers[i].subscriber.LastName, "Details", new { id = Model.subscribers[i].subscriber.SubscriberID })%> 
                </td> 
                <td> 
                    <%: Html.DisplayFor(M => M.subscribers[i].subscriber.Email)%> 
                </td> 
                <td> 
                    <%: Html.DisplayFor(M => M.subscribers[i].subscriber.Customer.Company)%> 
                    <%= Html.HiddenFor(M => M.subscribers[i].subscriber) %> 
                </td> 
            </tr> 
            <% } %>
            <% ViewBag.subscribers = Model.subscribers; %>                                    probeersel
            <%= Html.HiddenFor(model => model.subscribers) %>                                 probeersel
        </table>
        <%} %>
        <%else
           { %>
        <p>
            No subscribers found.</p>
        <%} %>
        <input type="submit" value="Add Subscribers" />
    </div>
    </fieldset>
            <%} %>

Give it a TRY....


Need Your Help

Can you apply a Media Query specific style to an HTML element via JavaScript?

javascript html css media-queries

Traditionally, you can use JavaScript to apply a style to an element:

How to call a php function like in codeigniter using ajax?

php codeigniter jquery

This one is very simple. In codeigniter I can have an ajax call like: