Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- View _AddEstEffort.cshtml (Modal implementation using Partial View)
- @model LMSPriorTool.ViewModels.EstimatedEffortViewModel
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
- <h3>Add new Estimated Effort</h3>
- </div>
- @using (Html.BeginForm("AddEstimatedEffort", "Project", FormMethod.Post, new { @class = "modal-form" }))
- {
- @Html.ValidationSummary()
- <div class="modal-body">
- <div class="row-fluid">
- <div>
- @Html.LabelFor(x => x.ProjectId)
- @Html.EditorFor(x => x.ProjectId)
- </div>
- <div>
- @Html.LabelFor(x => x.Cost)
- @Html.EditorFor(x => x.Cost)
- </div>
- </div>
- </div>
- <div class="modal-footer">
- <button class="btn btn-primary" type="submit">Save</button>
- <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
- </div>
- }
- 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:
- <script type="text/javascript">
- $(function () {
- //Optional: turn the chache off
- $.ajaxSetup({ cache: false });
- $('#btnAddEstimatedEffort').click(function () {
- $('#dialogContent').load(this.href, function () {
- $('#dialogDiv').modal({
- backdrop: 'static',
- keyboard: true
- }, 'show');
- bindForm(this);
- });
- return false;
- });
- });
- function bindForm(dialog) {
- $('form', dialog).submit(function () {
- $.ajax({
- url: this.action,
- type: this.method,
- data: $(this).serialize(),
- success: function (result) {
- if (result.success) {
- $('#dialogDiv').modal('hide');
- // Refresh:
- // location.reload();
- } else {
- $('#dialogContent').html(result);
- bindForm();
- }
- }
- });
- return false;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement