VS2012 で MVC4 と EF Data First を利用するプロジェクトに取り組んでいます。データベースには、2 つのフィールドで構成される複合キーを持つテーブルがあります。編集アクションが呼び出されると、オプションのデフォルト パラメータ id の値は常にゼロであり、選択されたレコードの値ではないため、DB からは何も返されません。テーブルに単一フィールドの主キーがある場合、パラメーターは正しく設定されています。これを修正する方法がわかりません。提案が役立ちます。ありがとう
public class GamesController : Controller
{
private Context db = new Context();
//
// GET: /Games/
public ActionResult Index()
{
var gms = from g in db.Games orderby g.Date, g.GameNumber ascending select g;
return View(gms);
// return View(db.Games.ToList());
}
//
// GET: /Games/Edit/5
public ActionResult Edit(int id = 0)
{
Games games = db.Games.Find(id);
if (games == null)
{
return HttpNotFound();
}
return View(games);
}
2 番目のパラメーターを追加しても機能しませんでした。
public ActionResult Edit(int id = 0, int sid = 0)
{
Games games = db.Games.Find(id, sid);
if (games == null)
{
return HttpNotFound();
}
return View(games);
}
データベーステーブル
[Games]
GameNumber (PK, int not null)
Date (date, not null)
HomeTeamId (FK, int , not null)
AwayTeamId (FK, int , not null)
HomeTeamScore(int, null)
AwayTeamScore(int, null)
FieldId(FK, int, null)
GameType(nvarchar(15), null)
Season_Id(PK, FK, int, not null)
CreateDate(date, null)
RouteConfig.cs
namespace RetryMVC1
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Games",
url: "Games/{action}/{id}&{sid}",
defaults: new { controller = "Games", action = "Index", sid = "", gid = "" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
意見
@model RetryMVC1.Models.Games
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Games</legend>
@Html.HiddenFor(model => model.GameNumber)
<div class="editor-label">
@Html.LabelFor(model => model.Date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Date)
@Html.ValidationMessageFor(model => model.Date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.HomeTeamId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HomeTeamId)
@Html.ValidationMessageFor(model => model.HomeTeamId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AwayTeamId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AwayTeamId)
@Html.ValidationMessageFor(model => model.AwayTeamId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.HomeTeamScore)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HomeTeamScore)
@Html.ValidationMessageFor(model => model.HomeTeamScore)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AwayTeamScore)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AwayTeamScore)
@Html.ValidationMessageFor(model => model.AwayTeamScore)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FieldId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FieldId)
@Html.ValidationMessageFor(model => model.FieldId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.GameType)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.GameType)
@Html.ValidationMessageFor(model => model.GameType)
</div>
@Html.HiddenFor(model => model.Season_Id)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
これが問題のビューです
@model IEnumerable<RetryMVC1.Models.Games>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.HomeTeamId)
</th>
<th>
@Html.DisplayNameFor(model => model.AwayTeamId)
</th>
<th>
@Html.DisplayNameFor(model => model.HomeTeamScore)
</th>
<th>
@Html.DisplayNameFor(model => model.AwayTeamScore)
</th>
<th>
@Html.DisplayNameFor(model => model.FieldId)
</th>
<th>
@Html.DisplayNameFor(model => model.GameType)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.HomeTeamId)
</td>
<td>
@Html.DisplayFor(modelItem => item.AwayTeamId)
</td>
<td>
@Html.DisplayFor(modelItem => item.HomeTeamScore)
</td>
<td>
@Html.DisplayFor(modelItem => item.AwayTeamScore)
</td>
<td>
@Html.DisplayFor(modelItem => item.FieldId)
</td>
<td>
@Html.DisplayFor(modelItem => item.GameType)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>