EntityFrameWork DataFirst 和 MVC 默认参数为 null

本文关键字:参数 null 默认 MVC DataFirst EntityFrameWork | 更新日期: 2023-09-27 18:35:11

我正在利用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>

EntityFrameWork DataFirst 和 MVC 默认参数为 null

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