0

我正在开发一个在 VS2012 中使用 MVC4 和 EF Data First 的项目。该数据库有一个表,该表具有由两个字段组成的复合键。当调用编辑操作时,可选的默认参数 id 始终具有零值,而不是所选记录的值,因此数据库不会返回任何内容。如果表具有单个字段主键,则正确填充参数。我不确定如何解决这个问题,任何建议都会有所帮助。谢谢

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);
    }

添加第二个参数不起作用。

    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)

路由配置.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>
4

1 回答 1

0

您需要将第二个参数添加到您的路由配置中。

于 2013-10-13T07:58:10.330 回答