I have and mvc3 application:
ActivityController:
[code]
public ActionResult Edit(int id)
{
Activity activity = db.Activities.Find(id);
PopulateProjectsDropDownList(activity.ProjectID);
activity = db.Activities
.Include(i => i.Employee)
.Where(i => i.ActivityID == id)
.Single();
PopulateAssignedEmployeeData(activity);
return View(activity);
}//end method
[HttpPost]
public ActionResult Edit(int? id, FormCollection formCollection, string[] selectedEmployees)
{
if (id == null)
{
return View("Index");
}
var actvityToUpdate = db.Activities
.Include(a => a.Employee)
.Where(i => i.ActivityID == id)
.Single();
if (TryUpdateModel(actvityToUpdate, "", null, new string[]{"Employees"} ))
{
try
{
UpdateActivityEmployeeData(selectedEmployees, actvityToUpdate);
db.Entry(actvityToUpdate).State = EntityState.Modified;
db.SaveChanges();
return Redirect("Index");
//if (string.IsNullOrWhiteSpace(actvityToUpdate.))
//{
//}
}
catch (Exception exception)
{
//Log the error (add a variable name after DataException)
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists save urself");
ModelState.AddModelError("",exception.Message);
// ModelState.AddModelError("",form );
}//end catch
}
PopulateAssignedEmployeeData(actvityToUpdate);
return View(actvityToUpdate);
}//end method
[/code]
Model Activity:
[code]
public class Activity
{
//[Key]
//[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
//[Display(Name = "Nummer(willekeurig nummer)")]
//[Key]
public int ActivityID { get; set; }
[Required(ErrorMessage = "Naam moet wel ingevuld worden")]
[MaxLength(50)]
[Display(Name = "Activiteit")]
public string Name { get; set; }
[Display(Name = "Omschrijving")]
public string Description { get; set; }
[Display(Name = "uren")]
public decimal Hour { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Begin datum")]
public DateTime BeginDate { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Eind datum")]
public DateTime EndDate { get; set; }
[Display(Name = "Tarief")]
public decimal Tariff { get; set; }
[Display(Name = "Actief?")]
public bool Active { get; set; }
[Display(Name = "project")]
public int ProjectID { get; set; }
//public virtual DbSet
public virtual Project Project { get; set; }
public virtual ICollection
//public virtual ICollection
public virtual ICollection
}
[/code]
But when I try to Edit the form, for example this Object:
http://localhost:56610/Activity/Edit/4
And I post, then I get this error:
Server Error in '/' Application.
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'ManyToMany.Controllers.ActivityController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
and the routing is then:
http://localhost:56610/Activity/Edit/Index
THX
albert albertPosted Jul 9, 2012, 4:21 AM
Because if I choose: http://localhost:56610/Activity/Edit/4
and I update it then I see the update in: http://localhost:56610/Activity.
But the routing is not working correct. Because if I press on the Save button then I will be redirect to: http://localhost:56610/Activity/Edit/Index.
instead of: http://localhost:56610/Activity/.
And this I have in the: Global.asax file:
[code]
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
// filters.Add(new HandleLoggingAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//new { ActivityID = @"\d+" }// Parameter defaults
);
/*
routes.MapRoute(
"Activity",
"Activity/{ActivityID}",
new { controller = "Activity", action = "Edit" },
new { ActivityID = @"\d+" }
);
*/
}
protected void Application_Start()
{
ModelBinders.Binders.Add(typeof(decimal),
new DecimalModelBinder());
Database.SetInitializer
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
[/code]
But I dont know how to change the routing?
Because for other objects it is working. I mean the editing function.
THX