2013-10-13 42 views
0

我正在使用VS2012中的MVC4和EF數據優先項目。數據庫有一個由兩個字段組成的組合鍵。當編輯動作被調用時,可選的默認參數ID總是具有零值,而不是所選記錄的值,因此沒有任何數據從DB返回。如果該表具有單個字段主鍵,則該參數正確填充。我不知道如何解決這個問題,任何建議都會有所幫助。謝謝EntityFrameWork DataFirst和MVC默認參數爲空

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> 
+0

我知道這很明顯,但是你是否將任何參數傳遞給了'public ActionResult Edit(int id = 0,int sid = 0)',或者你有'id = 0'和'sid = 0'的實際記錄?你也可以嘗試'Games games = db.Games.Where(g => g.id = id && g.sid = sid).ToList()'; – Leron

+0

@Leron這是我的確切問題,我無法弄清楚如何將參數傳遞給動作。我目前正在嘗試@DarthVader建議向路由配置文件添加第二個參數。順便說一下,我嘗試了您的查詢建議,但修改了一下。 '遊戲遊戲= db.Games.Where(g => g.Season_Id = id && g.GameNumber = sid).ToList();'我得到以下錯誤'「運算符'&&'不能應用於' int'and'int'「' – Gearbolt

+0

所以我添加了一個新的RouteConfig路由,參數仍然沒有通過。 '所以我添加了一個新的RouteConfig路由,參數仍然沒有通過。 'routes.MapRoute( name:「Games」, url:「Games/{action}/{id}/{sid}」, 默認值:new {controller =「Games」,action =「Index」,sid = 「」,gid =「」} );' – Gearbolt

回答

0

你需要第二個參數添加到您的路線配置的視圖。

+0

我會看看是否添加第二條路線。 'routes.MapRoute(name:「Games」,url:「Games/{action}/{id} - {sid}」,默認值:new {controller =「Games」,action =「Index」,id = 10,sid = 4} );' – Gearbolt

+0

因此,我添加了一個新的RouteConfig路由,參數仍然沒有通過。 'routes.MapRoute( name:「Games」, url:「Games/{action}/{id}/{sid}」, 默認值:new {controller =「Games」,action =「Index」,sid = 「」,gid =「」} );' – Gearbolt

+0

你可以發佈你如何創建鏈接? – DarthVader