3
我一直在做一些非常基本的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
有默認值(零和空白)的所有字段值。
爲什麼?我做錯了什麼?
打我吧......我只是寫出了這個相同的確切響應! – SlackerCoder
謝謝,很好的回答!現在可以請你解釋爲什麼這是一個不好的做法作爲這個問題的答案:http://stackoverflow.com/q/6509729/7850 –