我一直在创建一些非常基础的MVC视图来保存一些数据。到目前为止,我已经将它们全部基于LinqToSql类。现在,我第一次基于我自己创建的一个简单小类,称为Purchase
,创建了一个表单。
public class Purchase {
long UserID;
decimal Amount;
string Description;
}
创建了一个类似于这样的视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestViewer.Models.Purchase>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Test Purchase
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Test Purchase</h2>
<% using (Html.BeginForm("SavePurchase", "Home")) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.UserID) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.UserID,
SystemUser.Select(u=>new SelectListItem{ Text=u.UserName, Value=u.ID.ToString()}).OrderBy(s=>s.Text))%>
<%: Html.ValidationMessageFor(model => model.UserID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Amount) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Amount) %>
<%: Html.ValidationMessageFor(model => model.Amount) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Description) %>
<%: Html.ValidationMessageFor(model => model.Description) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
但是当我重新回到我的HomeController
代码中:
public ActionResult SavePurchase(Purchase p) {
// code here
}
对象p
的所有字段值都具有默认值(零和空白)。
为什么?我做错了什么吗?