locked
How can I open a new web page after I click a checkbox using c# asp net MVC ? RRS feed

  • Question

  • User-1453200658 posted

    Hi,

    How can I open a new web page after I click a checkbox using c# asp net MVC ?

    I need open the new view Index_p when selected checkbox Breakfast in the view Index

    My view Index

    <div class="row">
        <div class="col-md-12">
            <div class="form-group" style="background-color: darkorange; border:3px solid; font-weight:bold;">
                <h5 style="font-weight: bold;"> Breakfast: @Html.CheckBoxFor(m => m.Breakfast, true) </h5>
            </div>
        </div>
    </div>
    

    My model:

    public class PersonModel
    {
        public bool Breakfast { get; set; }
    }
    

    My controller:

    [HttpPost]
    public ActionResult Index(PersonModel person)
    {
        bool ischecked = person.Breakfast;
    
        if(ischecked == true)
            return RedirectToAction("Index_p");
    
        if (ModelState.IsValid)
        {
            return View(person);
        }
    }
    Tuesday, April 20, 2021 10:15 AM

Answers

  • User503812343 posted

    add a click event to checkbox

    <h5 style="font-weight: bold;"> Breakfast: @Html.CheckBoxFor(m => m.Breakfast, new {   @onclick="opennewwindow();" }) </h5>

    add script to open new window

    <script type="text/javascript">
        function opennewwindow() {
            window.open('url of new view');
        }
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 20, 2021 3:20 PM
  • User-474980206 posted

    or auto post back:

    <h5 style="font-weight: bold;"> Breakfast: @Html.CheckBoxFor(m => m.Breakfast, new { onclick="this.form.submit();" }) </h5>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 20, 2021 3:27 PM
  • User287926715 posted

    Hi Edward Sheriff Curtis,

    Do you want to add the Index_p page to the Index view after the checkbox is selected?

    Or do you want to pop up a modal after the checkbox is selected, and showing the content of the Index_p view?

    If you just want to open a new page after selecting the checkbox, you can refer to the following demo.

    View:

    @model WebApplication1.Models.PersonModel
    
    @{
        Layout = null;
    }
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <div class="row">
        <div class="col-md-12">
            <div class="form-group" style="background-color: darkorange; border:3px solid; font-weight:bold;">
                <h5 style="font-weight: bold"> Breakfast: @Html.CheckBoxFor(m => m.Breakfast, new { @onclick = "opennewwindow(this);" }) </h5>
            </div>
        </div>
    </div>
    
    <script>
        
          function opennewwindow(ele) {
                if (ele.checked) {
    
                window.location.href = "@Url.Action("Test03")"
                }
            }
    </script>

    Result:

    Best Regards,


    ChaoDeng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 21, 2021 6:49 AM

All replies

  • User503812343 posted

    add a click event to checkbox

    <h5 style="font-weight: bold;"> Breakfast: @Html.CheckBoxFor(m => m.Breakfast, new {   @onclick="opennewwindow();" }) </h5>

    add script to open new window

    <script type="text/javascript">
        function opennewwindow() {
            window.open('url of new view');
        }
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 20, 2021 3:20 PM
  • User-474980206 posted

    or auto post back:

    <h5 style="font-weight: bold;"> Breakfast: @Html.CheckBoxFor(m => m.Breakfast, new { onclick="this.form.submit();" }) </h5>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 20, 2021 3:27 PM
  • User287926715 posted

    Hi Edward Sheriff Curtis,

    Do you want to add the Index_p page to the Index view after the checkbox is selected?

    Or do you want to pop up a modal after the checkbox is selected, and showing the content of the Index_p view?

    If you just want to open a new page after selecting the checkbox, you can refer to the following demo.

    View:

    @model WebApplication1.Models.PersonModel
    
    @{
        Layout = null;
    }
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <div class="row">
        <div class="col-md-12">
            <div class="form-group" style="background-color: darkorange; border:3px solid; font-weight:bold;">
                <h5 style="font-weight: bold"> Breakfast: @Html.CheckBoxFor(m => m.Breakfast, new { @onclick = "opennewwindow(this);" }) </h5>
            </div>
        </div>
    </div>
    
    <script>
        
          function opennewwindow(ele) {
                if (ele.checked) {
    
                window.location.href = "@Url.Action("Test03")"
                }
            }
    </script>

    Result:

    Best Regards,


    ChaoDeng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 21, 2021 6:49 AM