locked
Submit / Save button destination RRS feed

  • Question

  • User2074931137 posted

    The "save" submit button brings user back to the index view (assignment.index).  I would like to bring user to an alternate view (assignment.contact).

    @model fvm.Models.Assignment
    
    @{
        ViewBag.Title = "Edit";
    }
    
    <h2>Edit</h2>
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>Assignment</h4>
            <hr />
            @Html.ValidationSummary(true)
            @Html.HiddenFor(model => model.AssignmentID)
    
            <div class="form-group">          
                @Html.LabelFor(model => model.ContactID, "Employee", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("ContactID", String.Empty)
                    @Html.ValidationMessageFor(model => model.ContactID)
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EquipmentID, "Vehicle", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("EquipmentID", String.Empty)
                    @Html.ValidationMessageFor(model => model.EquipmentID)
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.RouteID, "Route", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("RouteID", String.Empty)
                    @Html.ValidationMessageFor(model => model.RouteID)
                </div>
            </div>
    
            <div class="form-group">                                                <- This defaults back to the Assignment.Index View.
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
    
    <div>
        @Html.ActionLink("Back to List", "Contact")                                  <- This (Assignment.Contact) is the view the save button should go.
    </div>

    Wednesday, March 13, 2019 8:39 PM

Answers

  • User1520731567 posted

    Hi 3v3rhart,

    The "save" submit button brings user back to the index view (assignment.index).  I would like to bring user to an alternate view (assignment.contact).

    This depends on the return value of the POST action of submit button.

    For example:

    I guess your post action is like:

            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit(Assignment model)
            {
    
                  ...
                    if (ModelState.IsValid)
                    {
                        db.Entry(model).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("Index");//if you save successfully,it will jump to Index view
                    }
    
            
                  ...
                return View(model);//If the save failed,it will return to the Edit view.
    }

    if you would like to Contact view after clicking the "save" button,

    I think you just modify the code,like: 

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit(Assignment model)
            {
    
                  ...
                    if (ModelState.IsValid)
                    {
                        db.Entry(model).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("Contact");//if you save successfully,it will jump to Contact view
                    }
    
            
                  ...
                return View(model);//If the save failed,it still return to the Edit view.
    
            }

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 14, 2019 3:17 AM

All replies

  • User475983607 posted

    The "save" submit button brings user back to the index view (assignment.index).  I would like to bring user to an alternate view (assignment.contact).

    This is handled in the Action.  Remember, Views are NOT invoked directly.  Actions are methods that return Views.

    This ActionLink helper simply creates a link according to the input parameters you supplied.

    @Html.ActionLink("Back to List", "Contact")   

    if you want to generate a different link then add an "IF" that renders what you want or use variables.

    <div>
        @{ 
            string linkText = "Back to List";
            string action = "Contact";
            bool MyLinkRule = true;
    
            if (MyLinkRule == true)
            {
                action = "SomeOtherAction";
            }
        }
    
        @Html.ActionLink(linkText, action)                                  
    </div>

    Wednesday, March 13, 2019 8:51 PM
  • User1520731567 posted

    Hi 3v3rhart,

    The "save" submit button brings user back to the index view (assignment.index).  I would like to bring user to an alternate view (assignment.contact).

    This depends on the return value of the POST action of submit button.

    For example:

    I guess your post action is like:

            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit(Assignment model)
            {
    
                  ...
                    if (ModelState.IsValid)
                    {
                        db.Entry(model).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("Index");//if you save successfully,it will jump to Index view
                    }
    
            
                  ...
                return View(model);//If the save failed,it will return to the Edit view.
    }

    if you would like to Contact view after clicking the "save" button,

    I think you just modify the code,like: 

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit(Assignment model)
            {
    
                  ...
                    if (ModelState.IsValid)
                    {
                        db.Entry(model).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("Contact");//if you save successfully,it will jump to Contact view
                    }
    
            
                  ...
                return View(model);//If the save failed,it still return to the Edit view.
    
            }

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 14, 2019 3:17 AM