MVC 3 ModelView在view&之间传递参数调节器

时间:2012-01-16 14:49:18

标签: c# asp.net-mvc-3 .net-4.0

我在测试项目中一直在玩MVC 3并遇到以下问题。

我有Group&订阅者实体和那些通过SubscriberGroup表进行耦合。

使用Group的DetailView打开包含所有订阅者的SubscriberGroup视图。 此列表可以选择过滤。

到目前为止一切正常,但是当我在控制器上调用AddToGroup方法时它失败了。具体来说,它进入方法但不传递subscriberCheckedModels列表。 难道我做错了什么?

查看: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; }
    }
}

2 个答案:

答案 0 :(得分:1)

IEnumerable不会自动绑定,我们需要修改视图中的foreach循环,如下所示,

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

答案 1 :(得分:0)

首先让我们修改您的模型以获得Id字段

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

现在让我们修改控制器,

    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 });
            }


        }

现在让我们修改一下视图

<%@ 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>
            <%} %>

给它一个尝试....