Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database does not update with Create View ( Repost to new forum) 1

Status
Not open for further replies.

Turkbear

Technical User
Mar 22, 2002
8,631
US
Hi,
I am trying to learn ( at my advanced age) the MVC method of developing .NET web apps, but, in trying the tutorials here using virtual Web Developer 2008 Express :

I cannot seem to get the databse to update with new records.
I am using the Movie Database video tutorial i c# and get no error messages but , except for the List ( index) page which shows the records in my database, the other views/actions do not have any effect.
I have this HomeController

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MovieApp.Models;

namespace MovieApp.Controllers
{
    public class HomeController : Controller
    {
        private MoviesDBEntities _entities = new MoviesDBEntities();
     

        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View(_entities.MovieSet.ToList());
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Home/Create

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

        //
        // POST: /Home/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude="Id")]Movie movieToCreate)
        {
            try
            {
                // TODO: Add insert logic here
                _entities.AddToMovieSet(movieToCreate);
                _entities.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Home/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

And This is the Create view page:
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MovieApp.Models.Movie>" %>

<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="Title">Title:</label>
                <%= Html.TextBox("Title") %>
                <%= Html.ValidationMessage("Title", "*") %>
            </p>
            <p>
                <label for="Director">Director:</label>
                <%= Html.TextBox("Director") %>
                <%= Html.ValidationMessage("Director", "*") %>
            </p>
            <p>
                <label for="DateReleased">DateReleased:</label>
                <%= Html.TextBox("DateReleased") %>
                <%= Html.ValidationMessage("DateReleased", "*") %>
            </p>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

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

</asp:Content>

The database is the included SqlServer 2008 Express
and I can, using the Data Explorer, add records directly, just not from these pages.

What am I missing, or what additional info do you need to assist.
Thanks

[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi,
Just a note to those who provided me with great responses to this post when it was in the previous 'unified' .NET forum ( and I hope they copy them to this forum) -
This is reposted per Dave's request to place it in the proper forum, now that one exists.




[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Is there a prize for being the first :)

To others who read this Turkbear resolved the issue by first removing the try/catch block around the create method. this exposed the exception which was thrown. the data access layer was not configured to insert new movies. once configured everything ran smoothly.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi,
Just to give proper credit, it was Jason who provided me with the path to resolving this.
Thanks Jason [smile]
( I gave him a star in the other forum, and now he has one in this..)



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top