EntityFrameWork DataFirst和MVC默认参数为空

时间:2013-10-13 03:59:30

标签: c# asp.net-mvc entity-framework asp.net-mvc-4

我正在开发一个利用VS2012中的MVC4和EF Data First的项目。数据库有一个表,其中包含由两个字段组成的复合键。调用编辑操作时,可选的默认参数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);
    }

添加第二个参数无效。

    public ActionResult Edit(int id = 0, int sid = 0)
    {
        Games games = db.Games.Find(id, sid);
        if (games == null)
        {
            return HttpNotFound();
        }
        return View(games);
    }

DatabaseTable

[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>

1 个答案:

答案 0 :(得分:0)

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