MVC 3 ModelView passing parameters between view & controller
- by Tobias Vandenbempt
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; }
}
}