开发者

ScaffoldColumn(false) doesn't work

Goal: Display a website to fill out a form and also be enable to make a validation before clicking on the send button.

problem: The code [ScaffoldColumn(false)] doesn't work when I use <%: Html.EditorForModel() %> in the view.

Questions: What should I do?

// 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 I开发者_Go百科D="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; }
        } 
    }
}


it doesn't work because your view is not strongly typed to anything.

it should be

<%@ Page ... Inherits="System.Web.Mvc.ViewPage<Order>" %> 

also if you would have an Order.ascx in your editor templates, the ScaffoldColumn would also don't do anything

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜