views:

74

answers:

3

I am attempting to generate my first MVC application. I have a very basic table: Teams: ID,Name. I have created the MVC app ok and the table is listed. Below is the Create View. When it is run I get the message: A value is required. Can you help (sorry this is very basic).

View create.aspx:

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

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

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

<h2>Create</h2>

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Name">Name:</label>
            <%= Html.TextBox("Name") %>
            <%= Html.ValidationMessage("Name", "*") %>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

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

</asp:Content>

with controller teamcontroller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using GettingStarted.Models;
using DB = GettingStarted.Models.GettingStartedDataContext;

namespace GettingStarted.Controllers
{
    public class TeamController : Controller
    {
        // other actions
        ...
        //
        // GET: /Team/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Team/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Team team)
        {

            if (ModelState.IsValid)
            {
                try
                {
                    var db = new DB();
                    db.Teams.InsertOnSubmit(team);
                    db.SubmitChanges();
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(team);
                }
            }
            return View(team);
        }

    }
}
+2  A: 

Your Create view is strong type so provide a view model instance:

public ActionResult Create()
{
    return View(new Team());
}

or

public ActionResult Create()
{
    return View((Team)null);
}
Robert Koritnik
Thanks for your response. It made sense to pass the model instance. However it still complains 'Create was unsuccessful - a value is required'
I was advised to use post (ms video) [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Team team) { if (ModelState.IsValid) { try { var db = new DB(); db.Teams.InsertOnSubmit(team); db.SubmitChanges(); return RedirectToAction("Index"); } catch { return View(team); } } return View(team); }
@nprosser: could you also change the code in your question with the one that you used to get to the mentioned exception **and** attach stack trace of that particular error, so we can actually see what's going on.
Robert Koritnik
@nprosser: Advised to use post when? On GET you won't be using POST anyway...
Robert Koritnik
A: 

A hint: Also add a Create action, which takes Team as parameter, to handle validation errors.

public ActionResult Create(Team team)
{
    return View(team);
}

Also passing a null value to a create form isn't necessary! Your problem might be somewhere else. Can you try to use

<%= Html.TextBoxFor(model => model.Name) %>
<%= Html.ValidationMessageFor(model => model.Name) %>

instead of

<%= Html.TextBox("Name") %>
<%= Html.ValidationMessage("Name", "*") %>

?

yan.kun
This was already part of the controller: public ActionResult Create(Team team) { if (ModelState.IsValid) { try { var db = new DB(); db.Teams.InsertOnSubmit(team); db.SubmitChanges(); return RedirectToAction("Index"); } catch { return View(team); } } return View(team); }
Updated my answer, try this :)
yan.kun
when I replace with '<%= Html.TextboxFor(model => model.Name) %> <%= Html.ValidationMessageFor(model => model.Name) %> ' I get the messageCS1061: 'System.Web.Mvc.HtmlHelper<GettingStarted.Models.Team>' does not contain a definition for 'TextboxFor' and no extension method 'TextboxFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<GettingStarted.Models.Team>' could be found (are you missing a using directive or an assembly reference?)
@nprosser: well in that case you **must be using MVC 1** and not 2 as you indicated in your tags.
Robert Koritnik
I installed MVC2 well that was the intention. Will investigate - would you know a quick way to check the version?
Where is TextboxFor? what library? Thanks
Mark Dickinson
@nprosser: have you converted an existing MVC1 application to MVC2? Did you change web.config assembly references to correct versions?
Robert Koritnik
@Mark Dickinson: you can find them in the `System.Web.Mvc.dll` assembly under `System.Web.Mvc.Html` namespace. There are many many new extensions in MVC2. `TextBoxFor` is part of the `InputExtensions` static class.
Robert Koritnik
@Mark Dickinson: Unless you wanted to point out the lowercase typo. You could as well just say so. ;)
Robert Koritnik
+1  A: 

The problem may be an annotation on a field in the Model. Have you checked your Model for something like:

public class Team {

   [Required(ErrorMessage = "A value is required")]
   public string whatEver {get; set;}

   ...
}
Steve Shaffar