Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 2
| Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET MVC 2
This is the forum to discuss the Wrox book Professional ASP.NET MVC 2 by Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery; ISBN: Professional ASP.NET MVC 2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2011, 07:41 PM
Registered User
 
Join Date: Jul 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question Problem with ModelView and ModelState.IsValid on [HttpPost]Create

I am following the NerdDinner example, but creating my own project, and am having a problem with the [HttpPost]Create when using the ModelView pattern.

When I click Save with a blank form ModelState.IsValid is returning true and trying to save the blank object to the database.

Here are my model classes:

Code:
[MetadataType(typeof(Team_Validation))]
    public partial class Team { }

    public partial class Team
    {
        public Team()
        {
            this.createdBy = 9;
            this.createdOn = DateTime.Now;
            this.deleted_b = false;
        }
    }

    public class Team_Validation
    {
        [Required(ErrorMessage = "Name is required")]
        [StringLength(50, ErrorMessage = "Name may not be longer than 50 characters")]
        string Name { get; set; }

        [Required(ErrorMessage = "League is required")]
        int LeagueId { get; set; }
    }

    public class TeamFormViewModel
    {
        FFRepository data = new FFRepository();

        public Team Team { get; private set; }
        public SelectList Leagues { get; private set; }

        public TeamFormViewModel(Team team)
        {
            this.Team = team;
            this.Leagues = new SelectList(data.GetLeagues(), "LeagueId", "ListName", Team.LeagueId);
        }

    }
Here is the aspx file:

Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<FFAdminMvc.Models.TeamFormViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Create New Team
</asp:Content>

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

    <h2>Create New Team</h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            
            <div class="editor-label">
                Team Name
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Team.Name)%>
                <%: Html.ValidationMessageFor(model => model.Team.Name)%>
            </div>

            <div class="editor-label">
                League
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.Team.LeagueId, Model.Leagues, "")%>
                <%: Html.ValidationMessageFor(model => model.Team.Name) %>
            </div>
            
            <div>
                <input type="submit" value="Create" />
            </div>

        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>
Here are the Create methods from the controller:

Code:
  //
        // GET: /Team/Create
        public ActionResult Create()
        {
            Team team = new Team();
            return View(new TeamFormViewModel(team));
        } 

        //
        // POST: /Team/Create
        [HttpPost]
        public ActionResult Create(Team team)
        {
            if (ModelState.IsValid)
            {
                data.AddTeam(team);
                //data.Save();
                return RedirectToAction("Details", new { id = team.TeamId });
            }
            else
            {
                return View(new TeamFormViewModel(team));
            }
        }

Any help with what I'm missing would be greatly appreciated.

Thanks,

Mike

Last edited by bigasscactus; July 11th, 2011 at 07:47 PM.. Reason: Forgot the add the 2 Create controller methods.
 
Old July 14th, 2011, 01:09 PM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

Missing a [Bind(Include"Name, LeagueId")] after the [MetadataType]?

Nerd dinner example looks like this:
Code:
[MetadataType(typeof(Dinner_Validation))]
[Bind(Include="Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude")]

public partial class Dinner
{
 ...
}




Similar Threads
Thread Thread Starter Forum Replies Last Post
DinnersController.cs Detele HttpPost method parameter logiccool BOOK: Professional ASP.NET MVC 2 1 July 14th, 2011 12:56 PM
ADO Create Procedure problem xiangchendub Access VBA 3 January 23rd, 2008 09:37 AM
page.isvalid... pls help life_s Ng ASP.NET 1.0 and 1.1 Basics 3 November 10th, 2003 11:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.