Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 2
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 11th, 2011, 08: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 08:47 PM. Reason: Forgot the add the 2 Create controller methods.
  #2 (permalink)  
Old July 14th, 2011, 02: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
{
 ...
}
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 01: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



All times are GMT -4. The time now is 02:42 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.