Redirect to other View based on Button Clicked in ASP.Net MVC

Last Reply on Apr 30, 2019 09:30 PM By bigbear

Posted on Apr 30, 2019 12:04 AM

I have 2 buttons but I cant bring my model to the page and click a button to determine how to set my model. I’m thinking if I can get my model in my page I’ll be able to figure out these buttons possibly.

I tried bring the model like usual new { model = Model} but my model Is still null…

Here is my code 

        // GET: TimeSheet/AdminApproval{masterid}
        public ActionResult AdminApproval(int masterId)
        {
            var masterModel = context.TimeSheetMaster.Where(w =>
                              w.TimeSheetMasterId.Equals(masterId)).FirstOrDefault();
            var detailM = context.TimeSheetDetails.Where(t => t.TimeSheetMasterId.Equals(
                              masterModel.TimeSheetMasterId)).FirstOrDefault();
            var project = context.Projects.Where(p => p.ProjectId == 
                              detailM.ProjectId).FirstOrDefault();

            var details = (from master in context.TimeSheetMaster
                           join detail in context.TimeSheetDetails
                           on master.TimeSheetMasterId equals detail.TimeSheetMasterId
                           where master.TimeSheetMasterId == masterId
                           select new TimeSheetDetailsModel()
                           {
                               Sunday = detail.Sunday,
                               Monday = detail.Monday,
                               Tuesday = detail.Tuesday,
                               Wednesday = detail.Wednesday,
                               Thursday = detail.Thursday,
                               Friday = detail.Friday,
                               Saturday = detail.Saturday,
                               Hours = detail.Hours,
                               Comment = master.Comment,
                               ProjectName = project.ProjectName
                           }).FirstOrDefault();
            return View(details);
        }
        //POST: TimeSheet/AdminApproval{model}{approve}{reject}
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> AdminApproval(TimeSheetDetailsModel model, 
               string btnApprove, string btnReject)
        {
            var master = await context.TimeSheetMaster.Where(m => m.TimeSheetMasterId == 
                         model.TimeSheetMasterId).FirstOrDefaultAsync();

            if(btnApprove != null)
            {

                return RedirectToAction("TimeSheetList");
            }
            else if(btnReject != null)
            {
                
                return RedirectToAction("Index", "Home");
            }
            else
            {

            }
            return View();
        }

 

                        <div class="panel-footer">
                        @using (Html.BeginForm("AdminApproval", "TimeSheet",
                            new { model = Model }, FormMethod.Post))
                        {
                            @Html.AntiForgeryToken()
                            <input type="submit" class="btn btn-success" value="Approve" name="btnApprove" />
                            <input type="submit" class="btn btn-danger" value="Reject" name="btnReject" />
                        }

 

You are viewing reply posted by: dharmendr on Apr 30, 2019 12:16 AM.
Posted on Apr 30, 2019 12:16 AM

Hi bigbear,

Check this example. Now please take its reference and correct your code.

Assign a common name property to the buttons and then using the name property check the condition to redirect to the specified action.

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult AdminApproval(string ApproveReject)
    {
        if (ApproveReject == "Approve")
        {
            return RedirectToAction("TimeSheetList");
        }
        else if (ApproveReject == "Reject")
        {
            return RedirectToAction("Index", "Home");
        }
        return View();
    }
}

View

<div>
    <%using (Html.BeginForm("AdminApproval", "Home", FormMethod.Post)) {%>
    <input type="submit" class="btn btn-success" value="Approve" name="ApproveReject" />
    <input type="submit" class="btn btn-danger" value="Reject" name="ApproveReject" />
    <% } %>
</div>