0

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!

B. Fitzgerald
  • 135
  • 1
  • 16
  • check out the answers in this question https://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form. Maybe they can help you out. I can't think of a way off the top of my head that doesn't requires some sort of JS determination of which button you pushed. – Hopeless Jun 22 '17 at 15:37

1 Answers1

0

Hope this may help you

You can can set name of button.If click on that button then we can get which button is clicked.

In Create.cshtml

 <button type="submit" id="Create" name="Create"></button>
 <button type="submit" id="Close" name="Close"></button>

In Controller.cs

 Public ActionResult Create(string name){
 if(name="Create")
 {
 //here code for create button
   return RedirectToAction("Create", new { sub_num =project.SubmissionNumber });
 }
 if(name="Close")
 {
 //heere code for close button
  return JavaScript("window.close();");
 }
 }
ADNAN ZAMAN
  • 68
  • 1
  • 10