日期:2014-05-18  浏览次数:20452 次

mvc2中viewModel的问题
最近在看原版的Professinal ASP.net MVC 2。在介绍如何在视图中使用ViewModel绑定dropdownlist,但按书上的说法写好View、Controller和DDinnerFormViewModel后,运行时发现提交修改后无法保存更新。没有任何错误提示,就好像修改没有提交到数据库一样。不知道有没有朋友遇到过这种情况?

DinnerFormViewModel.cs代码:

namespace NerdDinner.Controllers
{
  public class DinnerFormViewModel
  {
  private static string[] _countries = new[] 
  {
  "USA",
  "UK",
  "CHN"
  };

  public Dinner Dinner { get; private set; }
  public SelectList Countries { get; private set; }

  public DinnerFormViewModel(Dinner dinner)
  {
  Dinner = dinner;
  Countries = new SelectList(_countries, dinner.Country);
  }
  }
}




Edit.aspx视图:

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

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit:<%: Model.Dinner.Title %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

  <h2>Edit</h2>

  <% using (Html.BeginForm()) {%>
  <%: Html.ValidationSummary("Please correct the errors and try again.") %>
  <fieldset>
  <legend>Fields</legend>
   
  <div class="editor-label">
  <%: Html.LabelFor(model => model.Dinner.Title) %>
  </div>
  <div class="editor-field">
  <%: Html.TextBoxFor(model => model.Dinner.Title, new { size = 30, @class = "myclass", style = "font-color:red;" })%>
  <%: Html.ValidationMessageFor(model => model.Dinner.Title, "*")%>
  </div>
   
  <div class="editor-label">
  <%: Html.LabelFor(model => model.Dinner.EventDate)%>
  </div>
  <div class="editor-field">
  <%: Html.TextBoxFor(model => model.Dinner.EventDate)%>
  <%: Html.ValidationMessageFor(model => model.Dinner.EventDate, "*")%>
  </div>
   
  <div class="editor-label">
  <%: Html.LabelFor(model => model.Dinner.Description)%>
  </div>
  <div class="editor-field">
  <%: Html.TextAreaFor(model => model.Dinner.Description)%>
  <%: Html.ValidationMessageFor(model => model.Dinner.Description, "*")%>
  </div>
   
  <div class="editor-label">
  <%: Html.LabelFor(model => model.Dinner.Address)%>
  </div>
  <div class="editor-field">
  <%: Html.TextBoxFor(model => model.Dinner.Address, "*")%>
  <%: Html.ValidationMessageFor(model => model.D