为什么我的ViewModel中没有传输数据?

时间:2010-11-28 12:55:02

标签: c# asp.net-mvc asp.net-mvc-2 viewmodel

嘿,我正在使用MVC2中的帮助台,我遇到的问题是数据没有通过我的ViewModel从我的视图中传输。

在主页上,我有三个DropDownLists,您可以从中选择问题区域。然后,您可以按一个按钮报告问题或转到常见问题解答。如果您在DropDownLists中选择了一个问题区域,则该站点应记住您通过视图模型进行数据传输时所选择的内容。

但是当我进入Home索引的Post方法时,我的SelectLists和SelectListItems为空,而包含按钮信息的字符串包含正确的数据。

我做错了什么?

这是我的家庭控制器:

public ActionResult Index()
    {
        var viewModel = new HomeIndexViewModel()
        {
            // The lists with problem areas are populated.
            problemAreas1 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"),
            problemAreas2 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"),
            problemAreas3 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName")
        };
        return View(viewModel);
    }

    //
    // POST: /Home/

    [HttpPost]
    public ActionResult Index(HomeIndexViewModel viewModel)
    {
        // snip
    }

我的观点模型:

public class HomeIndexViewModel
{
    // A SelectList containing elements for the DropDownLists in the Home "Index" view.
    public SelectList problemAreas1 { get; set; }
    public SelectList problemAreas2 { get; set; }
    public SelectList problemAreas3 { get; set; }

    // Items chosen in the DropDownLists.
    public SelectListItem itemOne { get; set; }
    public SelectListItem itemTwo { get; set; }
    public SelectListItem itemThree { get; set; }

    // String for IDing what button is pressed.
    public string submitButton { get; set; }
}

我的家庭索引视图(继承自HomeIndexViewModel,我只是没有包含代码,因为它搞砸了帖子):

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("Index", "Home", FormMethod.Post))
   {%>
<h2>
    Search for your problem:</h2>
<asp:Label ID="Label1" runat="server" Width="300px">
Choose a problem area, and search for an answer to your problem in the FAQ, 
    or you can press the report button to report your problem.</asp:Label>
<%-- Here are the DropDownLists. --%>
<p style="width: 220px; height: 24px;">
    <%: Html.DropDownListFor(model => Model.itemOne, Model.problemAreas1, "Choose a problem area")%>
</p>
<p style="width: 220px;">
    <%: Html.DropDownListFor(model => Model.itemTwo, Model.problemAreas2, "Choose a problem area")%>
</p>
<p style="width: 220px">
    <%: Html.DropDownListFor(model => Model.itemThree, Model.problemAreas3, "Choose a problem area")%>
</p>
<p>
    <%-- Here are the two buttons allowing people to do a search or create a new ticket respectively. --%>
    <input type="submit" value="Search for problems" name="submitButton" id="searchButton" />
    <input type="submit" value="Report problem" name="submitButton" id="submitButton" />
</p>
<%} %>

感谢您的时间。 : - )

3 个答案:

答案 0 :(得分:3)

当您将表单提交回POST操作时,problemAreas1,2,3属性将为null是完全正常的,因为它们的内容从未发布。您需要以与GET操作相同的方式重新填充它们。就itemOne,Two,Three属性而言,它们应该是简单的字符串值而不是SelectListItem类型。

型号:

public class HomeIndexViewModel
{
    // A SelectList containing elements for the DropDownLists in the Home "Index" view.
    public SelectList problemAreas1 { get; set; }
    public SelectList problemAreas2 { get; set; }
    public SelectList problemAreas3 { get; set; }

    // Items chosen in the DropDownLists.
    public string itemOne { get; set; }
    public string itemTwo { get; set; }
    public string itemThree { get; set; }

    // String for IDing what button is pressed.
    public string submitButton { get; set; }
}

控制器:

public ActionResult Index()
{
    var viewModel = new HomeIndexViewModel()
    {
        // The lists with problem areas are populated.
        problemAreas1 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"),
        problemAreas2 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"),
        problemAreas3 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName")
    };
    return View(viewModel);
}

//
// POST: /Home/

[HttpPost]
public ActionResult Index(HomeIndexViewModel viewModel)
{
    if (!ModelState.IsValid)
    {
        // the model wasn't valid =>
        // show the same form so that the user can fix validation errors
        viewModel.problemAreas1 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName");
        viewModel.problemAreas2 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName");
        viewModel.problemAreas3 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName");
        return View(viewModel);
    }
    // TODO: everything went fine => update your database and redirect
    return RedirectToAction("Index");
}

答案 1 :(得分:0)

我没有看到您的视图中的页面指令,让它知道您的模型?

这样的事情应该是你观点的第一行:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<HomeIndexViewModel>" %>

答案 2 :(得分:0)

您的选择列表将为null,因为该数据不会在表单中传回,这应该没问题。但是,因为它们为null,所以集合中没有SelectListItem MVC可以返回,因此它也是null。

尝试将以下内容添加到HomeIndexViewModel的构造函数中,而不是添加到控制器中(只是为了查看会发生什么)

problemAreas1 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"), 
problemAreas2 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"), 
problemAreas3 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName") 

我相信在返回POST时会发生绑定器将创建您的HomeIndexViewModel并填充SelectLists,并且应该能够填充您的SelectListItem

但有些东西似乎有点可疑。我的直觉告诉我,SelectListItem不应该为空。