On my "Create" view for my model, I'd like a "Submit" button as well as a "Submit & Close" button. I was looking at the answer to this question for information on how to close a page from a controller action (although I see some comments saying that this isn't possible--is there a workaround for that?).
These are the buttons currently:
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.Project.ProjectID);
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="save-buttons">
@if (Model.ReadOnly)
{
<button type="button" id="close_page" onclick="closeWindow();">Close</button>
}
else
{
<button type="submit">Save Project</button>
if (Model.Project == null)
{
<button type="button">Delete Project</button>
}
else
{
<button type="button" onclick="deleteProject(@Model.Project.ProjectID, @Model.Project.SubmissionNumber);">Delete Project</button>
}
<button type="button">Save & Close</button>
}
</div>
... the form itself is below here
and the submit action is:
public ActionResult Create([Bind(Include = "ProjectName,ProjectDescription,DateReceived,EffectiveDate,ExpirationDate,GeneralContractor,ProjectTerm,ProjectType,SubmissionNumber,PolicyNumber,Status,Underwriter,Division,BrokerCity,TAName,Branch,FirstNamedInsuredAddress,FirstNamedInsured,ProjectAddress")] Project project)
{
if (ModelState.IsValid)
{
if (project.ProjectID <= 0)
{
db.Projects.Add(project);
}
else
{
db.Entry(project).State = EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("Create", new { sub_num = project.SubmissionNumber });
}
var model = new ProjectViewModel()
{
Project = db.Projects.Find(project.ProjectID),
States = GetSelectListItems(GetAllStates()),
Branches = GetSelectListItems(GetAllBranches()),
Divisions = GetSelectListItems(GetAllDivisions()),
ProjectTypes = GetSelectListItems(GetAllProjectTypes()),
Statuses = GetSelectListItems(GetAllStatuses()),
ReadOnly = false,
Cleared = false,
EditMode = false
};
return View("Maintenance", model);
}
My problem comes with differentiating which button sent the browser to the submit method. I've come up with a few different possibilities but I'm not sure if any of them are possible (I don't think they are):
First is to create a boolean model property that is true when the page should close after submitting and false otherwise. Then I can wrap my RedirectToAction in an if statement like so:
if (Model.Done) {
return JavaScript("window.close();");
}
else
{
return RedirectToAction("Create", new { sub_num = project.SubmissionNumber });
}
But I don't know how I could set this property on button click. I don't think this is possible so I can't do this but correct me if I'm wrong.
The other possibility would be to pass an argument to the action when the button is clicked, but I don't know how to do this when it is a <button type="submit"></button>
.
Does anyone have any suggestions? Thanks!