Razor Page渲染页两次.net5

发布于 2025-02-04 21:45:00 字数 2841 浏览 3 评论 0 原文

CARS Razor Page是在第一次加载时两次调用 onget()。当我单击“查看”按钮<代码>视图剃须刀页面 Onget(int ID)两次。 service.submit 方法正在插入两个记录,而不是一个记录。 它不应该只调用每个页面方法一次吗?

cars.cshtml

@page 
@model IndexModel
@using MyApp.Models;

@{
    <table class="table">
        <thead>
            <tr>
                <th>Reference No</th>
                <th>Car Make</th>
                <th>Car Model</th>
                <th>Price</th>
                <th>Year</th>
            </tr>
        </thead>
        <tbody>
            @foreach (Car item in Model.CarList)
            {
                <tr>
                    <td>@item.ReferenceNo</td>
                    <td>@item.Make</td>
                    <td>@item.Model</td>
                    <td>@item.Price</td>
                    <td>@item.Year</td>
                    <td>
                        <a asp-page="View" asp-route-id="@item.id" class="btn btn-success btn-sm text-white">View</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}

cars.cshtml.cs

public class IndexModel : PageModel
{
    private readonly AppDbContext _context;
    public List<Car> CarList = new List<Car>();

    public IndexModel(AppDbContext context)
    {
        _context = context;
    }

    public void OnGet() // -----> this function is invoked twice.
    {
        try
        {
            CarList = _context.Car.FromSqlRaw("EXEC dbo.sp_GetCars").ToList();                
        }
        catch (Exception)
        {
            throw;
        }
    }
}

view.cshtml.cs


public class ViewModel : PageModel
    {
        private readonly AppDbContext _context;     
        private readonly IServiceScopeFactory _scopeFactory;

        public ViewModel(AppDbContext context, IServiceScopeFactory scopeFactory)
        {
            _context = context;
            _scopeFactory = scopeFactory;           
        }        
        public IActionResult OnGet(int id)
        {
            try
            {
                if (id != null) {
                    Service.Submit(id, _scopeFactory);  ------>Insert function is also invoked twice                   
                }
                return RedirectToPage("Index");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }

Cars razor page is invoking OnGet() twice on first load. When I click view button View razor page loads OnGet(int id) twice.
Service.Submit method is inserting two records instead of one.
Should it not call every page method only once ?

cars.cshtml

@page 
@model IndexModel
@using MyApp.Models;

@{
    <table class="table">
        <thead>
            <tr>
                <th>Reference No</th>
                <th>Car Make</th>
                <th>Car Model</th>
                <th>Price</th>
                <th>Year</th>
            </tr>
        </thead>
        <tbody>
            @foreach (Car item in Model.CarList)
            {
                <tr>
                    <td>@item.ReferenceNo</td>
                    <td>@item.Make</td>
                    <td>@item.Model</td>
                    <td>@item.Price</td>
                    <td>@item.Year</td>
                    <td>
                        <a asp-page="View" asp-route-id="@item.id" class="btn btn-success btn-sm text-white">View</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}

cars.cshtml.cs

public class IndexModel : PageModel
{
    private readonly AppDbContext _context;
    public List<Car> CarList = new List<Car>();

    public IndexModel(AppDbContext context)
    {
        _context = context;
    }

    public void OnGet() // -----> this function is invoked twice.
    {
        try
        {
            CarList = _context.Car.FromSqlRaw("EXEC dbo.sp_GetCars").ToList();                
        }
        catch (Exception)
        {
            throw;
        }
    }
}

view.cshtml.cs


public class ViewModel : PageModel
    {
        private readonly AppDbContext _context;     
        private readonly IServiceScopeFactory _scopeFactory;

        public ViewModel(AppDbContext context, IServiceScopeFactory scopeFactory)
        {
            _context = context;
            _scopeFactory = scopeFactory;           
        }        
        public IActionResult OnGet(int id)
        {
            try
            {
                if (id != null) {
                    Service.Submit(id, _scopeFactory);  ------>Insert function is also invoked twice                   
                }
                return RedirectToPage("Index");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2025-02-11 21:45:00

请允许我在这里写一个样本。我在CSHTML中有一个表格,并提供 Onpost 方法来处理它,然后使用依赖注入来注入服务来处理数据库插入方法。

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<form method="post">
    <table>
        <tr>
            <td>ID: </td>
            <td><input type="text" id="ID" name="ID"/></td>
        </tr>
        <tr>
            <td>Title: </td>
            <td><input type="text" id="Title" name="Title"/></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Submit buttom" /></td>
        </tr>
    </table>
    <hr/>
</form>


public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger; 
    private readonly ICommonService _commonService;
    public IList<Movie> Movie { get; set; }

    public IndexModel(ILogger<IndexModel> logger, ICommonService commonService)
    {
        _logger = logger; 
        _commonService = commonService;
    }

    public void OnGet()
    {
        Movie = _commonService.getData();
    }

    public void OnPost(Movie mov) {
        var a = mov.Title;
    }
}

Please allow me write a sample here. I had a form in cshtml and provide OnPost method to handle it, then using Dependency Injection to inject service to handle the database inserting method.

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<form method="post">
    <table>
        <tr>
            <td>ID: </td>
            <td><input type="text" id="ID" name="ID"/></td>
        </tr>
        <tr>
            <td>Title: </td>
            <td><input type="text" id="Title" name="Title"/></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Submit buttom" /></td>
        </tr>
    </table>
    <hr/>
</form>


public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger; 
    private readonly ICommonService _commonService;
    public IList<Movie> Movie { get; set; }

    public IndexModel(ILogger<IndexModel> logger, ICommonService commonService)
    {
        _logger = logger; 
        _commonService = commonService;
    }

    public void OnGet()
    {
        Movie = _commonService.getData();
    }

    public void OnPost(Movie mov) {
        var a = mov.Title;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文