Hi, I'm trying to pass the contents of the form to my Create method to create a new object in the database. When I hover over the button I can see that it points to the Controller/Method but when I click the button nothing happens. Here's what I have:
public ActionResult ProgressRecord(String studentName, String teacherName, String courseName, String assignmentDate)
{
ViewBag.Student = studentName;
ViewBag.Teacher = teacherName;
ViewBag.Course = courseName;
ViewBag.Date = assignmentDate;
var courseprogresses = from c in db.CourseProgresses where c.Student.Name == studentName && c.Teacher.Name == teacherName && c.Course.Name
== courseName select c;
int Count = (from c in courseprogresses select c.Assignment .Grade).Count();
ViewBag.ReturnedList = courseprogresses;
ViewBag.NewGrade = "You need to repeat the course.";
Have you tried debugging your application to see if it's actually firing data into the action method, or whether the brower isn't performing the post, at all? If you have text input boxes on your form, try entering data and hitting the enter key as this
should also perform a form post (this would indicate a problem with your submit button markup). Are you sure your braces are correct and match - if there's an extra one, this could be ending the form prematurely, before the submit button - you should also
check your rendered HTML to see that the closing form tag is after the submit button.
Please remember to mark replies as answers if you find them useful =8)
I'm using is mvc4 mvc development. I changed the following:
view:
@Html.ValidationSummary(true,"")
@using (Html.BeginForm((string)ViewBag.FormAction, "Create"))
@* create a folder where you view, corresponding to the controller is to create *@
{
<table>
Labels and input controls are here...
</table>
<input type ="submit" value ="Create" />
}
controller:
public ActionResult Create()
{
// CourseProgress cprogress =new CourseProgress();
// db.CourseProgresses.Add(cprogress);
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name");
ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name");
return View();
}
//
// POST: /ProgressManager/Create
[HttpPost]
public ActionResult Create(CourseProgress courseprogress)
{
if (ModelState.IsValid)
{
db.AddObject("CourseProgresses",courseprogress)
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", courseprogress.CourseId);
ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name", courseprogress.TeacherId);
return View(courseprogress);
}
I have nottestedyour reference,wehopeto help you.
AlexanderBla...
Member
325 Points
309 Posts
Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 04:23 AM|LINK
Hi, I'm trying to pass the contents of the form to my Create method to create a new object in the database. When I hover over the button I can see that it points to the Controller/Method but when I click the button nothing happens. Here's what I have:
@model SchoolIn.Models.CourseProgress
@{
ViewBag.Title = "Create a New Record)";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("Create", "ProgressManager", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.CourseProgressId)
@Html.ValidationSummary(true)
<table>
Labels and input controls are here...
</table>
<input type ="submit" value ="Create" />
}
Can someone tell me what I'm doing wrong? Thanks.
megh1207
Contributor
2274 Points
494 Posts
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 05:02 AM|LINK
things seems proper in view prehaps it might possible that you may have done some thing wrong in controller?
ignatandrei
All-Star
134989 Points
21641 Posts
Moderator
MVP
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 05:11 AM|LINK
How Create action looks like ? And make the class definition too...
AlexanderBla...
Member
325 Points
309 Posts
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 06:04 AM|LINK
Here's the controller...
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolIn.Models;
namespace SchoolIn.Controllers
{
public class ProgressManagerController : Controller
{
private SchoolInDB db = new SchoolInDB();
//
// GET: /ProgressManager/
public ViewResult Index()
{
var courseprogresses = db.CourseProgresses.Include(c => c.Course).Include(c => c.Teacher);
return View(courseprogresses.ToList());
}
//
// GET: /ProgressManager/Details/5
public ViewResult Details(int id)
{
CourseProgress courseprogress = db.CourseProgresses.Find(id);
return View(courseprogress);
}
//
// GET: /ProgressManager/ProgressRecord/student
public ActionResult ProgressRecord(String studentName, String teacherName, String courseName, String assignmentDate)
{
ViewBag.Student = studentName;
ViewBag.Teacher = teacherName;
ViewBag.Course = courseName;
ViewBag.Date = assignmentDate;
var courseprogresses = from c in db.CourseProgresses where c.Student.Name == studentName && c.Teacher.Name == teacherName && c.Course.Name == courseName select c;
int Count = (from c in courseprogresses select c.Assignment .Grade).Count();
ViewBag.ReturnedList = courseprogresses;
ViewBag.NewGrade = "You need to repeat the course.";
return View(courseprogresses);
}
//
// GET: /ProgressManager/Create
public ActionResult Create()
{
// CourseProgress cprogress =new CourseProgress();
// db.CourseProgresses.Add(cprogress);
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name");
ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name");
return View();
}
//
// POST: /ProgressManager/Create
[HttpPost]
public ActionResult Create(CourseProgress courseprogress)
{
if (ModelState.IsValid)
{
db.CourseProgresses.Add(courseprogress);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", courseprogress.CourseId);
ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name", courseprogress.TeacherId);
return View(courseprogress);
}
//
// GET: /ProgressManager/Edit/5
public ActionResult Edit(int id)
{
CourseProgress courseprogress = db.CourseProgresses.Find(id);
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", courseprogress.CourseId);
ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name", courseprogress.TeacherId);
var PdfReportProperties = new PdfReport();
return View(courseprogress);
}
//
// POST: /ProgressManager/Edit/5
[HttpPost]
public ActionResult Edit(CourseProgress courseprogress)
{
if (ModelState.IsValid)
{
var cp = db.CourseProgresses.Where(c => c.CourseProgressId == c.Parent.ParentId).First();
UpdateModel(cp);
db.SaveChanges();
return RedirectToAction("ProgressRecord");
}
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", courseprogress.CourseId);
ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name", courseprogress.TeacherId);
return View(courseprogress);
}
//
// GET: /ProgressManager/Delete/5
public ActionResult Delete(int id)
{
CourseProgress courseprogress = db.CourseProgresses.Find(id);
return View(courseprogress);
}
//
// POST: /ProgressManager/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
CourseProgress courseprogress = db.CourseProgresses.Find(id);
db.CourseProgresses.Remove(courseprogress);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
ignatandrei
All-Star
134989 Points
21641 Posts
Moderator
MVP
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 07:23 AM|LINK
1. Modify create
[httpPost]
public ActionResult Create(CourseProgress courseprogress)
{
if (ModelState.IsValid)
{
db.CourseProgresses.Add(courseprogress);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return Content("model state is not vali8d")
}
2. I doubt the create is succesfull since you have just an ID....
@Html.HiddenFor(model => model.CourseProgressId)
AlexanderBla...
Member
325 Points
309 Posts
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 07:49 AM|LINK
I’ve removed the @Html.HiddenFor(model => model.CourseProgressId) from the form and it does try to save. It stopped at the db.SaveChanges();
I tried using
var cp = db.CourseProgresses.Where(c => c.CourseProgressId == c.Parent.ParentId).First();
UpdateModel(cp);
db.CourseProgresses.Add(courseprogress);
db.SaveChanges();
return RedirectToAction("ProgressRecord");
…like you showed me earlier this week for the Edit Post method but got the The INSERT statement conflicted with the FOREIGN KEY constraint again.
ignatandrei
All-Star
134989 Points
21641 Posts
Moderator
MVP
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 08:15 AM|LINK
So WHAT do you POST to be saved ? Nothing?!
Mad-Halfling
Participant
1438 Points
729 Posts
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 09:41 AM|LINK
Have you tried debugging your application to see if it's actually firing data into the action method, or whether the brower isn't performing the post, at all? If you have text input boxes on your form, try entering data and hitting the enter key as this should also perform a form post (this would indicate a problem with your submit button markup). Are you sure your braces are correct and match - if there's an extra one, this could be ending the form prematurely, before the submit button - you should also check your rendered HTML to see that the closing form tag is after the submit button.
me0pro
Member
76 Points
76 Posts
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 10:34 AM|LINK
Hi AlexanderBlade :)
I'm using is mvc4 mvc development. I changed the following:
view:
{ <table> Labels and input controls are here... </table> <input type ="submit" value ="Create" /> }public ActionResult Create() { // CourseProgress cprogress =new CourseProgress(); // db.CourseProgresses.Add(cprogress); ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name"); ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name"); return View(); } // // POST: /ProgressManager/Create [HttpPost] public ActionResult Create(CourseProgress courseprogress) { if (ModelState.IsValid) { db.AddObject("CourseProgresses",courseprogress) db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", courseprogress.CourseId); ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "Name", courseprogress.TeacherId); return View(courseprogress); } I have not tested your reference, we hope to help you.AlexanderBla...
Member
325 Points
309 Posts
Re: Can't get my using BeginForm() to do a post. What am I doing wrong?
Feb 17, 2012 04:02 PM|LINK
The AdObject() does not seem to be in MVC3. I tried this:
db.AddObject("CourseProgress",courseprogress)
...there's no definition for it.