g.r.b
g.r.b

Reputation: 25

Link in partial Razor Page fires wrong OnGet

I am using a razor page to display an ItemT model and at the bottom I inserted a partial view to show all properties (which are not assigned to this model in a n:m relation).

The PageModel 'FreePropertiesToItemT' has two public properties:

public ItemT ItemT { get; set; }  // the one Item to show
public IList<PropertyIndexViewModel> FreeProperties { get; set; } // all free properties

An OnGetAsync(int? id) Method is called which works fine. The Page shows all data correctly.

The view displays a link for every Property:

<a asp-page-handler="addProperty" asp-route-id="@item.PropertyID">add</a>

This creates the link:

<a href="/Inventory/ItemTs/FreePropertiesToItemT?id=1&amp;handler=addProperty">add</a>

This is the correct link (I think). The route, id value and the handler are correct because there is a second OnGet Method in the PageModel:

public async Task<IActionResult> OnGetaddPropertyAsync(int? id)

However, the link only calls OnGetAsync (and not OnGetaddProppertyAsync) every time and, of course, for every Property! What am I missing?

Model of ItemT:

public class ItemT
{
    [Key]
    public int ItemTID { get; set; }

    [Required]
    [StringLength(100, MinimumLength = 1)]
    [Display(Name = "ItemT")]
    public string Title { get; set; }
    public bool isActive { get; set; } = true;
    public virtual ICollection<ItemTProperty> ItemTProperties { get; set; }
}

ViewModel of free properties:

public class PropertyIndexViewModel
{
    [Key]
    public int PropertyID { get; set; }

    [Required]
    [StringLength(100, MinimumLength = 1)]
    public string Title { get; set; }
    public bool DefaultsOnly { get; set; }

    [Display(Name = "Unit")]
    public string Unit { get; set; }

    [Display(Name = "Valuetype")]
    public string Valuetype { get; set; }
}

The Page to list one ItemT:

@page
@model Inventory.Areas.Inventory.Pages.ItemTs.FreePropertiesToItemTModel

@{
    ViewData["Title"] = "FreePropertiesToItemT";
}

<h1>Free Properties</h1>

<div>
    <h4>ItemT</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.ItemT.Title)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.ItemT.Title)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.ItemT.isActive)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.ItemT.isActive)
        </dd>
    </dl>
</div>
<div>
    <a asp-page="./Edit" asp-route-id="@Model.ItemT.ItemTID">Edit</a> |
    <a asp-page="./Index">Back to List</a>
</div>
<p></p>

<div>
    @{
        ViewData["FreeProperties"] = true;
    }
    <partial name="../Properties/_Properties.cshtml" model="Model.FreeProperties" />
</div>

The Partial which is loaded:

@using Inventory.DAL.ViewModels
@model IList<PropertyIndexViewModel>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model[0].Title)
            </th>
            <th>
                @Html.DisplayNameFor(model => model[0].DefaultsOnly)
            </th>
            <th>
                @Html.DisplayNameFor(model => model[0].Unit)
            </th>
            <th>
                @Html.DisplayNameFor(model => model[0].Valuetype)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DefaultsOnly)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Unit)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Valuetype)
                </td>
                <td>
                    @if (ViewBag.FreeProperties != null)
                    {
                    <a asp-page-handler="addProperty" asp-route-id="@item.PropertyID">add</a>
                    }
                </td>
            </tr>
        }
    </tbody>
</table>

And the c# code behind the page:

namespace Inventory.Areas.Inventory.Pages.ItemTs
{
    public class FreePropertiesToItemTModel : PageModel
    {
        private readonly IUnitOfWork _uow;

        public FreePropertiesToItemTModel(IUnitOfWork uow)
        {
            _uow = uow;
        }

        public ItemT ItemT { get; set; }
        public IList<PropertyIndexViewModel> FreeProperties { get; set; }

        public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            ItemT = await _uow.ItemTRepo.getById((int)id);

            if (ItemT == null)
            {
                return NotFound();
            }            
            FreeProperties = await _uow.PropertyRepo.getFreePropertiesForItemT((int)id);

            return Page();
        }

        public async Task<IActionResult> OnGetaddPropertyAsync(int? id)
        {
            if( id == null)
            {
                return NotFound();
            }
            if(ItemT == null) { return NotFound(); }
            await _uow.ItemTRepo.addProperty(ItemT.ItemTID, (int)id);
            await _uow.Commit();
            return Page();
        }
    }
}

Upvotes: 1

Views: 898

Answers (1)

Xueli Chen
Xueli Chen

Reputation: 12715

The issue is that your handler name error ,change it like below:

public async Task<IActionResult> OnGetAddPropertyAsync(int? id)

The first letter of handler name must be capitalized , otherwise handler=addProperty in the url is treated as a query-string parameter not a handler name.

Upvotes: 1

Related Questions