开发者

Asp.Net MVC Create/Update/Delete with composite key

开发者 https://www.devze.com 2022-12-25 09:27 出处:网络
I\'m not sure how to use composite key. My Categories table has CategoryId (PK,FK), Langua开发者_C百科geId (PK,FK), CategoryName

I'm not sure how to use composite key.

My Categories table has CategoryId (PK,FK), Langua开发者_C百科geId (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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消