2016-09-05 22 views
-2

自動生成された編集/詳細アクションリンク内にモデルIDが表示されません。代わりに、代わりにhostname/controller/idにリンクします。ホスト名/コントローラ/詳細/ idASP MVCモデルIDがアクションリンクに渡されません

ほとんどの場合、自動生成されたコードを使用します。あまり変更されていない - 検索の追加だけで&ソートなど

私はそれが何か信じられないほどばかげていることは間違いない!

みんなありがとう

モデル:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 

namespace SMS_ADM.Models 
{ 
    public class Logger 
    { 

     [Key] 
     public int Id { get; set; } 

     [Required(ErrorMessage = "Serial is required - This is the ID number of the weather station.")]   
     [DisplayName("Station Serial")]   
     public int Serial { get; set; } 

     [DisplayName("Friendly Name")] 
     public string Friendlyname { get; set; } 

     [Required(ErrorMessage = "Location is required - Please select the location where the weather station physically resides.")] 
     [DisplayName("Location")] 
     public int Locationid { get; set; } 

     [DisplayName("Location")] 
     public virtual Location Location { get; set; }  

    } 
} 

コントローラー:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.Mvc; 
using SMS_ADM.Models; 
using System.Web.WebPages.Html; 

namespace SMS_ADM.Controllers 
{ 
    public class LoggersController : Controller 
    { 
     private SmsadmContext db = new SmsadmContext(); 

     // GET: Loggers 
     public ActionResult Index() 
     { 
      var loggers = db.Loggers.Include(l => l.Location); 

     return View(loggers.ToList()); 

      // return View(); 

     } 

     // GET: Loggers/Details/5 
     public ActionResult Details(int? Id) 
     { 
      if (Id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Logger logger = db.Loggers.Find(Id); 
      if (logger == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(logger); 
     } 

     // GET: Loggers/Create 
     public ActionResult Create() 
     { 
      ViewBag.locationid = new System.Web.Mvc.SelectList(db.Locations, "Id", "locationname"); 
      return View(); 
     } 

     // POST: Loggers/Create 
     // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
     // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create([Bind(Include = "serial,friendlyname,locationid")] Logger logger, Logger model) 
     { 
      ViewBag.locationid = new System.Web.Mvc.SelectList(db.Locations, "Id", "locationname"); 

      if (ModelState.IsValid) 
      { 

       if (db.Loggers.Any(o => o.Serial == logger.Serial)) 
       { 
        //exists 
        ModelState.AddModelError(string.Empty, "Weather station serial number already exists in system."); 
        return View(); 
       } 
       else 
       { 
        db.Loggers.Add(logger); 
        db.SaveChanges(); 
        return RedirectToAction("Index"); 
       } 
      } 
      return View(logger); 
     } 

     // GET: Loggers/Edit/5 
     public ActionResult Edit(int? Id) 
     { 
      if (Id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Logger logger = db.Loggers.Find(Id); 
      if (logger == null) 
      { 
       return HttpNotFound(); 
      } 
      ViewBag.locationid = new SelectList(db.Locations, "Id", "locationcode", logger.Locationid); 
      return View(logger); 
     } 

     // POST: Loggers/Edit/5 
     // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
     // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit([Bind(Include = "Id,serial,friendlyname,locationid")] Logger logger) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(logger).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      ViewBag.locationid = new SelectList(db.Locations, "Id", "locationcode", logger.Locationid); 
      return View(logger); 
     } 

     // GET: Loggers/Delete/5 
     public ActionResult Delete(int? Id) 
     { 
      if (Id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Logger logger = db.Loggers.Find(Id); 
      if (logger == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(logger); 
     } 

     // POST: Loggers/Delete/5 
     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public ActionResult DeleteConfirmed(int Id) 
     { 
      Logger logger = db.Loggers.Find(Id); 
      db.Loggers.Remove(logger); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       db.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 
    } 
} 

ビュー:

@model IEnumerable<SMS_ADM.Models.Logger> 

@{ 
    ViewBag.Title = "Weather Stations List"; 
} 

<h2>@ViewBag.title</h2> 

<p> 
    @Html.ActionLink("Register New Weather Station", "Create") 
</p> 
@using (Html.BeginForm()) 
{ 
    <p> 
     Search: @Html.TextBox("SearchString") 
     <input type="submit" value="Search" /> 
    </p> 
} 
<table class="table"> 
    <tr> 
     <th> 
      @Html.ActionLink("DBID", "Index", new { sortOrder = ViewBag.lcodeSortParm }) 
     </th> 
     <th> 
      @Html.ActionLink("Location Code", "Index", new {sortOrder = ViewBag.lcodeSortParm}) 
     </th> 
     <th> 
      @Html.ActionLink("Serial", "Index", new {sortOrder = ViewBag.serialSortParm}) 
     </th> 
     <th> 
      @Html.ActionLink("Friendly Name", "Index", new {sortOrder = ViewBag.fnameSortParm}) 
     </th> 
     <th> 
      @Html.ActionLink("Site", "Index", new {sortOrder = ViewBag.siteSortParm}) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Id) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.Location.Locationcode) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.Serial) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Friendlyname) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.Location.Site) 
     </td> 

     <td> 
      @Html.ActionLink("Edit", "Edit", new {Id = item.Id}, null) | 
      @Html.ActionLink("Details", "Details", new {Id = item.Id}) | 
      @Html.ActionLink("Delete", "Delete", new {Id = item.Id}) 
     </td> 
    </tr> 
} 

</table> 

答えて

0

[OK]を - 私はこれを投稿した後、数分以内にそれを見つけました...

私と同じ問題を抱えている人は、これを確認する必要があります。

私はRouteConfig.csファイルを変更してIDを取り出しました。 -

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{Id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 

へ:だから私は、この変更します

関連する問題