Basically i have created a mvc application that will be performing CRUD operations on the database data using entity framework generated from database edmx.So in this after adding EDM and configuring database settings i've added a controller named CRUD with scaffolding enabled to read/write action views and attached model and DB conttext to it.Now everything works as desired upto the point of adding new employee ie Create.The problem comes when i run the index page of crud controller and in which when i press the actionlink edit or delete for any specific employee data it doesn't find that Edit and Delete cshtml pages at all and instead shows the error 404 resource not found.Please help me figure out what's wrong,i'm posting my whole code below:
public class CRUDController : Controller
{
private TestDBEntities2 db = new TestDBEntities2();
public ActionResult Index()
{
return View(db.Emps.ToList());
}
public ActionResult Details(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Emp emp)
{
if (ModelState.IsValid)
{
db.Emps.Add(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp);
}
public ActionResult Edit(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Emp emp)
{
if (ModelState.IsValid)
{
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp);
}
public ActionResult Delete(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Emp emp = db.Emps.Find(id);
db.Emps.Remove(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
Index.cshtml
#model IEnumerable<MvcApplication2.Emp>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.EmployeeID)
</th>
<th>
#Html.DisplayNameFor(model => model.Firstname)
</th>
<th>
#Html.DisplayNameFor(model => model.Lastname)
</th>
<th>
#Html.DisplayNameFor(model => model.Age)
</th>
<th>
#Html.DisplayNameFor(model => model.Project)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Firstname)
</td>
<td>
#Html.DisplayFor(modelItem => item.Lastname)
</td>
<td>
#Html.DisplayFor(modelItem => item.Age)
</td>
<td>
#Html.DisplayFor(modelItem => item.Project)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { /*id=item.PrimaryKey */ }) |
#Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
Create.cshtml
#model MvcApplication2.Emp
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Emp</legend>
<div class="editor-label">
#Html.LabelFor(model => model.EmployeeID)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EmployeeID)
#Html.ValidationMessageFor(model => model.EmployeeID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Firstname)
#Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Lastname)
#Html.ValidationMessageFor(model => model.Lastname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Age)
#Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Project)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Project)
#Html.ValidationMessageFor(model => model.Project)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Edit.cshtml
#model MvcApplication2.Emp
#{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Emp</legend>
<div class="editor-label">
#Html.LabelFor(model => model.EmployeeID)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EmployeeID)
#Html.ValidationMessageFor(model => model.EmployeeID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Firstname)
#Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Lastname)
#Html.ValidationMessageFor(model => model.Lastname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Age)
#Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Project)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Project)
#Html.ValidationMessageFor(model => model.Project)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Image of Solution Explorer:
pass EmployeeID in your ActionLinks:
#Html.ActionLink("Edit", "Edit", new {id=item.EmployeeID }) |
#Html.ActionLink("Details", "Details", new {id=item.EmployeeID }) |
#Html.ActionLink("Delete", "Delete", new {id=item.EmployeeID })
try to add the Controller Name
#Html.ActionLink("Edit", "Edit", "CRUD", new {/*id=item.PrimaryKey */ }) |
#Html.ActionLink("Details", "Details", "CRUD", new { /*id=item.PrimaryKey */ }) |
#Html.ActionLink("Delete", "Delete", "CRUD", new { /*id=item.PrimaryKey */ })
Related
i'm trying to edit a row (my project is a simple phone book)from my index view which shows all of my records (Contact) but when i click on the edit button nothing happens
this is my delete method
#region [- Delete -]
#region [- Get -]
[HttpGet]
// [HttpDelete]
public ActionResult Delete(int? _id, Models.EF_Model.Phone_book _model)
{
return View();
}
#endregion
#region [- Post -]
[HttpPost]
//[HttpDelete]
public ActionResult Delete(Models.EF_Model.Phone_book _Model)
{
if (ModelState.IsValid)
{
Ref_ViewModel = new ViewModel.ViewModel();
Ref_ViewModel.Delete(_Model.Id);
}
else
{
ViewBag.Massage = "Choose a Contact";
}
return View(_Model);
}
#endregion
#endregion
this is my edit method in my Home controller
[HttpGet]
public ActionResult Edit(int? _id)
{
if (_id==null)
{
return new HttpStatusCodeResult(HttpStatusCode.NoContent);
}
else
{
Ref_ViewModel = new ViewModel.ViewModel();
return View(Ref_ViewModel.Select(_id));
}
}
[HttpPost]
public ActionResult Edit(ViewModel.DTO.Contact Ref_Contact)
{
if (ModelState.IsValid)
{
Ref_ViewModel = new ViewModel.ViewModel();
Ref_ViewModel.Edit(Ref_Contact, Ref_Contact.Id);
}
else
{
ViewBag.Message = "Choose a Contact";
}
return View();
}
this is it's view(Contact class is a simple DTO class)
#model Phone_Book.ViewModel.DTO.Contact
#{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Contact</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.FName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Num, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Num, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Num, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Address, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Address, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
this is my index view
#model IEnumerable<Phone_Book.Models.EF_Model.Phone_book>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.First_Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Last_Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Number)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.First_Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Last_Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Number)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
In your Edit method in your HomeController, try this:
[HttpGet]
public ActionResult Edit(int? _id)
{
if (_id==null)
{
return new HttpStatusCodeResult(HttpStatusCode.NoContent);
}
else
{
return View();
}
}
From your Home/Index View make a link to edit action of your HomeController
Index.cshtml
#model IEnumerable<Phone_Book.ViewModel.DTO.Contact>
<h1>Welcome!</h1>
#{
ViewBag.Title = "Home";
}
<table>
<thead>
<tr>
<th>Num</th>
<th>FirstName</th>
<th>LastName</th>
<th>Email</th>
<th>Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach (var contact in Model)
{
<tr>
<td>#contact.Num</td>
<td>#contact.FName</td>
<td>#contact.LName</td>
<td>#contact.Address</td>
<td>#Html.ActionLink("Edit", "Edit", new {id = contact.Id}) </td>
</tr>
}
</tbody>
</table>
I am a beginner on MVC and I am trying to Create a web app where I want the user to be able to create new items and view them in the same page.
I am trying to do this by using partial view for the Create Action inside the Index view.
The problem is I am not able to redirect from the child to the Index to update the list after creating new item. And I'm getting this error(Child actions are not allowed to perform redirect actions.)
Here is my model
public class Expense
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
[DataType(DataType.Currency)]
public decimal Amount { get; set; }
//[Required]
public string ApplicationUserId { get; set; }
}
,here are my Index and Create Actions
public ActionResult Index()
{
return View(db.Expenses.ToList());
}
public ActionResult Create()
{
return PartialView("Create", new Expense());
//return View();
}
// POST: /Expense/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="Id,Title,Amount,ApplicationUserId")] Expense expense)
{
if (ModelState.IsValid)
{
expense.ApplicationUserId = User.Identity.GetUserId();
db.Expenses.Add(expense);
db.SaveChanges();
return RedirectToAction("Index"); // Here is where The exception is thrown
}
return View();
}
And here is my Index view
#model IEnumerable<HomeManager.Models.Expense>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.Amount)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Amount)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
#Html.ActionLink("Details", "Details", new { id = item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
<p>
#Html.ActionLink("Create New", "Create")
#{Html.RenderAction("Create", "Expense");}
</p>
And here is my Create view
#model HomeManager.Models.Expense
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Expense</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(model => model.Title, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Amount, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Amount)
#Html.ValidationMessageFor(model => model.Amount)
</div>
</div>
#*<div class="form-group">
#Html.LabelFor(model => model.ApplicationUserId, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ApplicationUserId)
#Html.ValidationMessageFor(model => model.ApplicationUserId)
</div>
</div>*#
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default"/>
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The error is occurring because you html for the form is generating
<form action="/Expense/Index" method="post">
not action="/Expense/Create" as it needs to be. If you put a breakpoint on both the Index() GET and Create(Expense expense) POST methods, you will see that your hitting both of them when you submit the form.
To solve this, explicitly set the action and controller names in the BeginForm() method of the partial view
#using (Html.BeginForm("Create", "Expense"))
Note that since your Create() GET method is not performing any logic, you can also use
#{ Html.RenderPartial("Create", new Expense()); }
in lieu of RenderAction
If you are trying to display the new records immediately they are created then I suggest you use a Javascript library like Knockout, Angular Js or Ember.
Alternatively if you want to use only MVC then you will need to make an ajax call to a method that returns a partial view of the updated Expense record and place this partial view in your DOM in your view.
My Controller
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Customers Customer)
{
if (ModelState.IsValid)
{
_db.Customers.Add(Customer);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(Customer);
}
My Model:
public class Customers
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set;}
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
// public ICollection<CustomerData> Customers { get; set; }
}
From my View:
<p>
#Html.ActionLink("Create New", "Create")
Create.cshtml:
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Add New Customer</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.City)
#Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.State)
#Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Zip)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Zip)
#Html.ValidationMessageFor(model => model.Zip)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Phone)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Phone)
#Html.ValidationMessageFor(model => model.Phone)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
When I run this code, all of the fields end up getting rendered from the subsequent Create.cshtml. Filling in all of the forms and clicking the submit button sends me back to "Index", but does not add any of my fields into the database. It seems like the Create action is not being hit. Anyone have any advice for a beginner?
First off, thanks for everyone responding so quickly. Turns out, I had my db set to deploy on debug somehow. When I unchecked deploy on my project debug properties, everything worked.
I have this controller and what I am trying to do is to send an image to the controller as a [byte], this is my controller:
[HttpPost]
public ActionResult AddEquipment(Product product, HttpPostedFileBase image)
{
if (image != null)
{
product.ImageMimeType = image.ContentType;
product.ImageData = new byte[image.ContentLength];
image.InputStream.Read(product.ImageData, 0, image.ContentLength);
}
_db.Products.Add(product);
_db.SaveChanges();
return View();
}
and on my view:
#using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post)) {
<fieldset>
<legend>Product</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Price)
#Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Category)
#Html.ValidationMessageFor(model => model.Category)
</div>
<div>
<div>IMAGE</div>
<input type="file" name="image" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
but the problem is that on my controller the value for image is alway null, I cant seem to get any information on the HttpPostedFileBase
You need to add the encType with multipart/form-data.
#using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post, new {enctype = "multipart/form-data" })) {
You can always add it to your model too as follows, providing it is a ViewModel:
public class Product
{
public Product()
{
Files = new List<HttpPostedFileBase>();
}
public List<HttpPostedFileBase> Files { get; set; }
// Rest of model details
}
You can the retrieve the files by removing the un-needed parameter i.e.
[HttpPost]
public ActionResult AddEquipment(Product product)
{
var file = model.Files[0];
...
}
Try to do this at the top of the Action method:
[HttpPost]
public ActionResult AddEquipment(Product product, HttpPostedFileBase image)
{
image = image ?? Request.Files["image"];
// the rest of your code
}
And the form should have enctype of "multipart/form-data" to upload files:
#using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post, new {enctype = "multipart/form-data" })) {
I am working on a project utilizing MVC4 and EF Data First in VS2012. The database has a table with a composite key comprised of two fields. When the edit action is called the optional default parameter id always has the value of zero and not the value of the selected record so nothing is returned from the DB. If the table has a single field primary key the parameter is populated correctly. I am not sure how to fix this, any suggest would help. Thanks
public class GamesController : Controller
{
private Context db = new Context();
//
// GET: /Games/
public ActionResult Index()
{
var gms = from g in db.Games orderby g.Date, g.GameNumber ascending select g;
return View(gms);
// return View(db.Games.ToList());
}
//
// GET: /Games/Edit/5
public ActionResult Edit(int id = 0)
{
Games games = db.Games.Find(id);
if (games == null)
{
return HttpNotFound();
}
return View(games);
}
Adding a second parameter did not work.
public ActionResult Edit(int id = 0, int sid = 0)
{
Games games = db.Games.Find(id, sid);
if (games == null)
{
return HttpNotFound();
}
return View(games);
}
DatabaseTable
[Games]
GameNumber (PK, int not null)
Date (date, not null)
HomeTeamId (FK, int , not null)
AwayTeamId (FK, int , not null)
HomeTeamScore(int, null)
AwayTeamScore(int, null)
FieldId(FK, int, null)
GameType(nvarchar(15), null)
Season_Id(PK, FK, int, not null)
CreateDate(date, null)
RouteConfig.cs
namespace RetryMVC1
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Games",
url: "Games/{action}/{id}&{sid}",
defaults: new { controller = "Games", action = "Index", sid = "", gid = "" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
View
#model RetryMVC1.Models.Games
#{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Games</legend>
#Html.HiddenFor(model => model.GameNumber)
<div class="editor-label">
#Html.LabelFor(model => model.Date)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Date)
#Html.ValidationMessageFor(model => model.Date)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HomeTeamId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HomeTeamId)
#Html.ValidationMessageFor(model => model.HomeTeamId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AwayTeamId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.AwayTeamId)
#Html.ValidationMessageFor(model => model.AwayTeamId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HomeTeamScore)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HomeTeamScore)
#Html.ValidationMessageFor(model => model.HomeTeamScore)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AwayTeamScore)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.AwayTeamScore)
#Html.ValidationMessageFor(model => model.AwayTeamScore)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.FieldId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FieldId)
#Html.ValidationMessageFor(model => model.FieldId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.GameType)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.GameType)
#Html.ValidationMessageFor(model => model.GameType)
</div>
#Html.HiddenFor(model => model.Season_Id)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
This is the view in question
#model IEnumerable<RetryMVC1.Models.Games>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th>
#Html.DisplayNameFor(model => model.HomeTeamId)
</th>
<th>
#Html.DisplayNameFor(model => model.AwayTeamId)
</th>
<th>
#Html.DisplayNameFor(model => model.HomeTeamScore)
</th>
<th>
#Html.DisplayNameFor(model => model.AwayTeamScore)
</th>
<th>
#Html.DisplayNameFor(model => model.FieldId)
</th>
<th>
#Html.DisplayNameFor(model => model.GameType)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.HomeTeamId)
</td>
<td>
#Html.DisplayFor(modelItem => item.AwayTeamId)
</td>
<td>
#Html.DisplayFor(modelItem => item.HomeTeamScore)
</td>
<td>
#Html.DisplayFor(modelItem => item.AwayTeamScore)
</td>
<td>
#Html.DisplayFor(modelItem => item.FieldId)
</td>
<td>
#Html.DisplayFor(modelItem => item.GameType)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
You need to add the second parameter to your route config.