Reputation: 1031
I have a MVC 5 application. I follow up this tutorial Getting Started with ASP.NET MVC 5
Altough when I click Details, Edit or Delete I get HTTP Error 400.0 - Bad Request.
My Index view
@foreach (var item in Model)
{
<div class="city">
<h2>@Html.DisplayFor(modelItem => item.announce_PublishDate)</h2>
<p>@Truncate(item.announce_Description, 90)</p>
<p>@Html.ActionLink("more", "Details", new { id = item.announce_ID }) </p></div>}
My Details view
<div>
<h4>prokirikseis</h4>
<hr />
<dl class="dl-horizontal">
<dd>
@Html.DisplayFor(model => model.announce_ID)
</dd>
<dd>
@Html.DisplayFor(model => model.announce_Description)
</dd>
<dd>
@Html.DisplayFor(model => model.announce_PublishDate)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.announce_ID }) |
@Html.ActionLink("Back to List", "Index")</p>
the controller
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
announce announce= db.announce.Find(id);
if (announce == null)
{
return HttpNotFound();
}
return View(announce);
}
the model
public class announcements
{
[Key]
public String announce_ID { get; set; }
[Display(Name = "Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> announce_PublishDate { get; set; }
[Display(Name = "Title")]
public String announce_Description { get; set; }
}
What I am doing wrong? I notice that it brings the right ID in the url. http://localhost:zzzzz/ltest/Details/01_2.2 but it does't bring the content. thank you
Upvotes: 0
Views: 535
Reputation:
Your action methods are accepting a parameter which is typeof int
but the key in class announcements
is string
, not int
. (and the url
in your question is passing a value of 01_2.2
to the Details()
method so id
is null
).
Change your methods to
public ActionResult Details(string id)
although I recommend you actually change the key to be an auto incremented int
Upvotes: 3