目标: 显示一个网站以填写表格,并且还可以在点击发送按钮之前进行验证。
问题:
当我在视图中使用[ScaffoldColumn(false)]
时,代码<%: Html.EditorForModel() %>
不起作用。
问题: 我该怎么办?
// Fullmetalboy
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.Models.Order>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Address and Payment
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) {%>
<h2>Address and Payment</h2>
<fieldset>
<legend>Shipping Information</legend>
<%: Html.EditorForModel() %>
</fieldset>
<input type="submit" value="Submit Order" />
<% } %>
</asp:Content>
private BokButikEntities _myBokButikEntities = new BokButikEntities();
//
// GET: /Checkout/
public ActionResult AddressAndPayment()
{
return View();
}
//
// POST: /Checkout/AddressAndPayment
[HttpPost]
public ActionResult AddressAndPayment(FormCollection values)
{
var order = new Order();
TryUpdateModel(order);
try
{
var cart = ShoppingCart.GetCart(this.HttpContext);
order.Anvandarnamn = User.Identity.Name;
order.OrderDatum = DateTime.Now;
order.TotalKostnad = cart.KalkyleraTotalKostnad();
//Save Order
_myBokButikEntities.AddToOrders(order);
_myBokButikEntities.SaveChanges();
//Process the order
cart.CreateOrder(order);
return RedirectToAction("Complete", new { id = order.OrderID });
}
catch
{
//Invalid - redisplay with errors
return View(order);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace BokButik1.Models.Validation
{
[MetadataType(typeof(OrderMetadata))]
public partial class Order
{
// Validerar data
[Bind(Exclude = "OrderID")]
public class OrderMetadata
{
[Required(ErrorMessage = "Förnamn saknas")]
[DisplayName("Förnamn")]
[StringLength(50)]
public object Fornamn { get; set; }
[Required(ErrorMessage = "Efternamn saknas")]
[DisplayName("Efternamn")]
[StringLength(50)]
public object Efternamn { get; set; }
[Required(ErrorMessage = "Mobilnummer saknas")]
[DisplayName("Mobilnummer")]
public int Mobilnummer { get; set; }
[Required(ErrorMessage = "Gatuadress saknas")]
[DisplayName("Gatuadress")]
[StringLength(50)]
public object Gatuadress { get; set; }
[Required(ErrorMessage = "Postnummer saknas")]
[DisplayName("Postnummer")]
public object Postnummer { get; set; }
[Required(ErrorMessage = "Ort saknas")]
[DisplayName("Ort")]
[StringLength(50)]
public object Ort { get; set; }
[ScaffoldColumn(false)]
public object OrderID { get; set; }
[ScaffoldColumn(false)]
public object OrderDatum { get; set; }
[ScaffoldColumn(false)]
public object TotalKostnad { get; set; }
[ScaffoldColumn(false)]
public object Anvandarnamn { get; set; }
}
}
}
答案 0 :(得分:1)
它不起作用,因为您的视图没有强烈输入任何内容。
应该是
<%@ Page ... Inherits="System.Web.Mvc.ViewPage<Order>" %>
如果你的编辑器模板中有Order.ascx,那么ScaffoldColumn也不会做任何事情