2

I'm not sure how to use composite key.

My Categories table has CategoryId (PK,FK), LanguageId (PK,FK), CategoryName

CategoryId | LanguageId | CategoryName
1          | 1          | Car
1          | 2          | Auto
1          | 3          | Automobile
etc.

I'm following this design

The default action looks like

//
// GET: /Category/Edit/5

public ActionResult Edit(int id)
{
    return View();
}

and ActionLink

<%= Html.ActionLink("Edit", "Edit", new { id= item.CategoryId }) %>

Should I use something like

<%= Html.ActionLink("Edit", "Edit", new { id= (item.CategoryId + "-" + item.LanguageId) }) %>

so the url is

/Category/Edit/5-5

and

//
// GET: /Category/Edit/5-5

public ActionResult Edit(string id)
{
    // parse id

    return View();
}

or change the route to something like

/Category/Edit/5/5

Or there is some better way?

1
  • I tried to construct a key like this, and while it worked initially, it gave me problems later on in POST actions. It tried to push the constructed key into the id field of my model, I think because of a MVC automatism, which was wrong, and gave me an invalid ModelState. Commented Dec 21, 2017 at 13:06

2 Answers 2

4

Well, it's easier than I thought :-) Just put 2 parameters into RouteValues in ActionLink so it generates a query string.

<%= Html.ActionLink("Edit", "Edit", new { id= item.CategoryId, lang= item.LanguageId }) %>

The url will be Category/Edit/1?lang=3

So it's more about routing than anything else in my question. More on this

Sign up to request clarification or add additional context in comments.

3 Comments

Also, for your controller, you have to update it to something like this: db.NewArticleRequests.Find(id, userId); instead of db.NewArticleRequests.Find(id);. In my case, as I was writing my app, I decided to add userId to the PK of the table, along the previous bare Id so this generated an error on both EF and ASP.NET MVC controller actions that requested entities by PK.
Very nice. This works much better than constructing a key and using that as the id Parameter.
What is the shape of the controller?
0

Yes, there is a better way. You can also determine the user's language and culture from the request to determine which record from your database to use. And I wouldn't come up with a random scheme like 1 = english 2 = german. Use the standard culture identifiers.

1 Comment

You are right. I understand that I can use ie. website.com/en/... But what if I need to see all languages for CategoryName at once in my administration?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.