Question:
How to show value on a second razor page passed from the first?

Solution:

If you want to add a page with certain values, load that page, and then submit the form with those values. I've made a demo to help you reproduce this problem.


[BindProperty]

        public UserModel User { get; set; }


        public IActionResult OnPost()

        {

            if (!ModelState.IsValid)

            {

                return Page();

            }


            return RedirectToPage("Add",

                new { User.EntryDate, User.Initials });

        }

Add.cshtml.cs

public class AddModel : PageModel

    {

       

        [BindProperty]

        [FromForm]

        public ItemModel Item { get; set; } = new ItemModel();


        public void OnGet(string Initials, DateTime EntryDate)

        {

            Item.Initials = Initials;

            Item.EntryDate = EntryDate;

        }


        public void OnPost()

        {

            // do some actions......

        }

    }

Add.cshtml

@page

@model RazorP.Pages.AddModel

@{

}


<form method="post">

    <div asp-validation-summary="ModelOnly" class="text-danger"></div>

    <div class="row">

        <div class="col-md-4">

            Initials: @Html.DisplayFor(x=>x.Item.Initials)

            <input type="hidden" asp-for="Item.Initials" />

            <br />

            Entry Date: @Html.DisplayFor(x=>x.Item.EntryDate)

            <input type="hidden" asp-for="Item.EntryDate" />

        </div>

    </div>


    <div class="form-group-with-label login-input-password mb-2">

        <label>Age</label>

        <input class="form-control-input" asp-for="Item.Age" autocomplete="off" />

        <span class="fa fa-eye"></span>

        <span asp-validation-for="Item.Age" class="text-danger"></span>

    </div>


    <div class="form-group-with-label login-input-password mb-2">

        <label>Address</label>

        <input  class="form-control-input" asp-for="Item.Address" autocomplete="off" />

        <span class="fa fa-eye" ></span>

        <span asp-validation-for="Item.Address" class="text-danger"></span>

    </div>

    <button>Submit</button>

</form>



Suggested blogs:

>>How to save python yaml and load a nested class?-Python

>>How to send multiple HTML form fields to PHP arrays via Ajax?

>>How to Set up the Android Emulator

>>How to Set up the local environment for Angular development?

>>How to solve encoding issue when writing to a text file, with Python?



Ritu Singh

Ritu Singh

Submit
0 Answers