0

What is the best way to keep "references" between entities in CRUD operations through Action calls?

Let's say, I need to create a new product for the current client. How should I "transfer" the PK of my current client into the form of the new product in a different view.

At the Moment I'm using the Session container to store the PK of the client when the page is visited, but this way might be hard to maintain after several Actions.

Any idea about this? Does ASP.NET MVC provide anything for this kind of tracking?

Code sample:

For example to set a key when clicking somewhere in the gridview:

<script type="text/javascript">
    function OnFocusedCampaignRowChanged(s, e) {
        debugger;
        var tmp = s.GetRowValues(s.GetFocusedRowIndex(), "CampaignPK", SelectCampaignCallback);

    }
    function SelectCampaignCallback(values) {
        debugger;
        $.ajax({
            type: 'GET',
            url: '/Session/SetSessionKey',
            data: { key: "campaignPK", value: values[0] },
            sucess: function (response) {
                //alert("OK!");
                //alert(values[0]);
            },
            error: function (xhr) {
                //alert("ERROR:xhr");
                alert('ERROR::SetSessionKey!' + xhr.responseText);
            }
        });
    }
</script>

And then in my views I check things like this:

@if ((Session["ProductList"] as List<BaseProductViewModel>) != null)
{
    @Html.Partial("ProductListPartial", (List<BaseProductViewModel>)Session["ProductList"]);
}
else
{
    @Html.Partial("EditableListPartialView");
}

Or:

settings.CallbackRouteValues = new
{
    Controller = "CheckMonitor",
    Action = "ClientSelected",
    brandPK = Session["clientPK"]
};
blfuentes
  • 2,731
  • 5
  • 44
  • 72

1 Answers1

0

If when you refer to a 'Client', you're talking about the current logged in user and you want to get their ID, refer to this: Get current user id in ASP.NET Identity 2.0

If you're wanting to store a client ID on a product, you should be using a foreign key between your models, or a linking model. You don't want to be manually passing IDs around like that when you could just get it from your model.

Community
  • 1
  • 1
KDizzle
  • 51
  • 6