View _AddEstEffort.cshtml (Modal implementation using Partial View)
@model LMSPriorTool.ViewModels.EstimatedEffortViewModel
@using (Html.BeginForm("AddEstimatedEffort", "Project", FormMethod.Post, new { @class = "modal-form" }))
{
@Html.ValidationSummary()
@Html.LabelFor(x => x.ProjectId)
@Html.EditorFor(x => x.ProjectId)
@Html.LabelFor(x => x.Cost)
@Html.EditorFor(x => x.Cost)
}
View: Layout.cshtml (Included link to open modal)
@Html.ActionLink("Add Estimated Effort", "AddEstimatedEffort", new { projectId = Model.ProjectId }, new { id = "btnAddEstimatedEffort", @class = "btn btn-info pull-right" })
ViewModel: EstimatedEffortViewModel
public class EstimatedEffortViewModel
{
public int Cost { get; set; }
public int ProjectId { get; set; }
}
Controller: ProjectController.cs
public ActionResult AddEstimatedEffort(int projectId = 1)
{
EstimatedEffortViewModel efvm = new EstimatedEffortViewModel
{
ProjectId = projectId,
};
return PartialView("_AddEstEffort", efvm);
}
[HttpPost]
public ActionResult AddEstimatedEffort(EstimatedEffortViewModel estimatedEffortVM)
{
Project project = db.Projects.Find(estimatedEffortVM.ProjectId);
if (project == null)
{
return HttpNotFound();
}
project.EstimatedEfforts.Add(new EstimatedEffort
{
EstimatedEffortId = estimatedEffortVM.EstimatedEffortId,
Cost = estimatedEffortVM.Cost,
});
db.SaveChanges();
return RedirectToAction("Details", new { id = estimatedEffortVM.ProjectId });
}
JavaScript to open Modal: