ViewModel 为空 MVC2

发布于 2024-09-12 07:07:36 字数 4141 浏览 2 评论 0原文

当我按下提交按钮并访问控制器时,我的 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; } 
}

提前感谢您的帮助。

克莱尔

When I press the submit button and access the Controller my ViewModel is empty. I don't know why this is happening, I have similar code to this on the other views and they do pass through data.

Controller

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

Accessed when 'Submit' button hit

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

Thanks in advance for any help.

Clare

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2024-09-19 07:07:36

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

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

尝试

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

The problem I can see (and I fully expect others to correct me here) is the following:
(edited for clarity)

  1. Id won't be set because you are posting to a different form, and there is no hidden field for the ID, so it won't be set.
  2. Description won't be set, as there is nothing for it to be set from.
  3. Categories won't be set, because the Ienumerable is populated only in your get, and not your post (which is fair enough, as you aren't posting the whole list).
  4. Category isn't populated because you've named the drop down list "Categories", and so it won't match the name "Category" in the viewmodel.

Try

<%=Html.DropDownList("Category", Model.Categories)%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文