Home > Software engineering >  Error redirecting to page in ASP.NET Core razor pages
Error redirecting to page in ASP.NET Core razor pages

Time:12-27

I want to redirect to this page enter image description here

but I got this error :

InvalidOperationException: No page named 'Miner/MinerDetail' matches the supplied values.

I want to redirect to this page miner/MinerDetail with a model that it is minerPartsView

CodePudding user response:

Want to redirect to this page miner/MinerDetail with a model that it is minerPartsView

Well, as per your shared screenshot, it appeared that, you were not able to redirect to your MinerDetails page due to two main reason.

Firstly, if your Request.IsAjaxRequest() executes then your next block will not executes, you have already returned the statements.

Second, reason you are not redirecting to the MinerDetails page in correct manner. As you haven't share your MinerDetails page details design thus I am sharing how you could redirect to a new razorpage with new object.

Model:

Let's assume, we have below model which we would like to pass from our intial page to MinerDetails page.

public class MinerCustomModel
    {

        public string? PowerSerialNumber { get; set; }
        public string? MinerSerialNumber { get; set; }
        public string? WorkerName { get; set; }
    }

Note: In your scenario, model would be minerPartsView model

Intial Index/ Loading Page:

public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }
       
        public async Task<IActionResult> OnGet()
        {
            var myData = new MinerCustomModel()
            {
                MinerSerialNumber = "SM-001",
                PowerSerialNumber = "PSN-002",
                WorkerName = "Worker Name"
            };

            string data = System.Text.Json.JsonSerializer.Serialize(myData);
            return RedirectToPage("Miner/MinerDetails", new  { objectData = data });

           
        }
    }

MinerDetails Page:

public class MinerDetailsModel : PageModel
    {
        [BindProperty]
        public MinerCustomModel? minerCustomModel { get; set; } = new MinerCustomModel();

       
        public void OnGet(string objectData)
        {
            
            var enitity = System.Text.Json.JsonSerializer.Deserialize<MinerCustomModel>(objectData);

            minerCustomModel.PowerSerialNumber = enitity.PowerSerialNumber;
            minerCustomModel.MinerSerialNumber = enitity.MinerSerialNumber;
            minerCustomModel.WorkerName = enitity.WorkerName;
           
         
        }
    }

Note: Make sure, we are defining the MinerCustomModel which should a class not pageModel with [BindProperty] which should intializing as new MinerCustomModel() to avoid null reference exception.

MinerDetailsModel Page View:

@page
@model MinerDetailsModel


<h1>Miner Details</h1>


<table >
    <thead>
        <tr>
            <th>
                PowerSerialNumber
            </th>
            <th>
                MinerSerialNumber
            </th>
            <th>
                WorkerName
            </th>
        </tr>
    </thead>
    <tbody>
       
            <tr>
                <td>
                @Model.minerCustomModel?.PowerSerialNumber
                </td>
                <td>
                @Model.minerCustomModel?.MinerSerialNumber
                </td>
                <td>
                @Model.minerCustomModel?.WorkerName
                </td>
            </tr>
       
    </tbody>
</table>

Note: In MinerDetails view we should refer @model MinerDetailsModel page model not the MinerCustomModel or IndexModel.

Output:

enter image description here

  • Related