ViewModel是空的MVC2

时间:2010-07-29 09:43:16

标签: asp.net-mvc-2 viewmodel

当我按下提交按钮并访问Controller时,我的ViewModel为空。我不知道为什么会这样,我在其他视图上有类似的代码,他们确实传递了数据。

控制器

        public ActionResult DeleteCategory(int id)
        {
            var data = _service.GetIndividualCategory(id);
            if (data == null) return View("NotFound");

            // If attached to traders send to other view
            var traders = _service.GetTradersAttachedToCategory(id);
            if (!traders.Any())
            {
                var category = new DeleteCategoryViewModel
                {
                    Description = data.Description,
                    Id = data.Id
                };

                return View(category);
            }

            var trader = new DisplayTradersAttachedToCategoryViewModel
            {
                Description = data.Description,
                Id = data.Id,
                BusinessName = traders.Select(x => new BusinessNameViewModel { BusinessName = x.BusinessName, Id = x.Id }),
                Categories = _service.GetCategories(id).Select(x => new SelectListItem {Text = x.Description, Value = x.Id.ToString()})
            };

            return View("DeleteCategoryAttachedToTraders", trader);
        }

点击“提交”按钮后访问

       [HttpPost]
        public ActionResult DeleteCategoryFromTradersAttachNewCategory(DisplayTradersAttachedToCategoryViewModel displayTradersAttachedToCategoryViewModel)
        {
            if (displayTradersAttachedToCategoryViewModel.Button == "Back to List") return RedirectToAction("ViewCategories");

            //Update traders with new category
            //delete category

            //if (_service.DeleteCategory((int)deleteCategoryViewModel.Id))
            //{
            //    return RedirectToAction("ViewCategories");
            //}

            return View("DeleteCategoryAttachedToTraders", displayTradersAttachedToCategoryViewModel);
        }

DeleteCategoryAttachedToTraders.aspx

<%@ Page Title="Delete Category - Traders Attached to Category" Language="C#" MasterPageFile="~/Views/Shared/Master.Master" Inherits="System.Web.Mvc.ViewPage<Internal.ViewModels.Controller.DisplayTradersAttachedToCategoryViewModel>" %>

<asp:Content ID="pageTitle" ContentPlaceHolderID="PageTitle" runat="server">
  Delete Category - Traders Attached to Category
</asp:Content>

<asp:Content ID="htmlHead" ContentPlaceHolderID="HtmlHead" runat="server">
  <link href="<%=ResolveUrl("~/Assets/Styles/CSS.css")%>" rel="Stylesheet" type="text/css" />
</asp:Content>

<asp:Content ID="bodyContent" ContentPlaceHolderID="BodyContent" runat="server">
  <div class="center"><%
    using (Html.BeginForm("DeleteCategoryFromTradersAttachNewCategory", "Controller",FormMethod.Post))
    {%>    
      <h2>Delete Category - Traders Attached to Category</h2>
      <div class="magiPadBig"></div>

      <div class="magiPadBig"></div>      
      <%=Html.DisplayFor(x => x.BusinessName)%>

      <div class="magiPadBig"></div>   
      <h3>New Category</h3>

      <div class="magiPadBig"></div>    
      <%=Html.DropDownList("Categories")%>

      <div class="magiPadBig">  
        <input type="submit" name="Button" value="Back to List" />
        <input type="submit" name="Button" value="Submit" />
      </div><%
    }%>
  </div>
</asp:Content>  

DisplayTradersAttachedToCategoryViewModel

public class DisplayTradersAttachedToCategoryViewModel
{
    public int Id { get; set; }
    public string Description { get; set; }

    [UIHint("BusinessNameDisplayTemplate")]
    public IEnumerable<BusinessNameViewModel> BusinessName { get; set; }

    public IEnumerable<SelectListItem> Categories { get; set; }

    public int Category { get; set; }

    public string Button { get; set; } 
}

提前感谢您的帮助。

克莱尔

1 个答案:

答案 0 :(得分:1)

我能看到的问题(我完全希望其他人在这里纠正我)如下: (为清晰起见编辑)

  1. 我不会设置ID,因为您要发布到其他表单,并且ID没有隐藏字段,因此不会设置。
  2. 将不会设置说明,因为没有任何内容可以设置。
  3. 不会设置类别,因为Ienumerable仅在您的get中填充,而不是您的帖子(这是公平的,因为您没有发布整个列表)。
  4. 未填充类别,因为您已将下拉列表命名为“类别”,因此它与视图模型中的“类别”名称不匹配。
  5. 尝试

    <%=Html.DropDownList("Category", Model.Categories)%>