I've run into an error 404, which implicates that I lack certain resource.
I'm unexperienced with ASP.NET MVC really and routing is the where I suck most, though this is unexcpected error for me as I have the required controller + action.
1 Raport has many ThrashType records. They are seperate tables, though it doesn't matter at this point I think.
The problem is that I get the correct view and records of Raport table.
When I hover link in the view it has format {host}/Raport/EditRecords/{string_id}
When I click the link in the view (trying to go to EditRecords) I get 404 error which is confusing atm.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Home",
"Edit/{productId}",
new { controller = "Home", action = "Edit" },
new { productId = @"\w+" }
);
}
Code is as follows:
controller EditRaport:
public ActionResult EditRaport()
{
var query = from dbs in db.Raport
select dbs;
return View(query as IEnumerable<Raport>);
}
controller EditRecords:
public ActionResult EditRecords(string id)
{
var query = from dbs in db.ThrashType
where dbs.raport_id == id
select dbs;
return View(query as IEnumerable<ThrashType>);
}
view for EditRaport:
@model IEnumerable<WebApplication1.Klasy_Raport.Raport>
@{
ViewBag.Title = "EditRaport";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit Raport</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserFrontInfo.userName)
</th>
<th>
@Html.DisplayNameFor(model => model.creation_time)
</th>
<th>
@Html.DisplayNameFor(model => model.last_modyfication)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserFrontInfo.userName)
</td>
<td>
@Html.DisplayFor(modelItem => item.creation_time)
</td>
<td>
@Html.DisplayFor(modelItem => item.last_modyfication)
</td>
<td>
@Html.ActionLink("Edit", "EditRecords", new { id=item.raport_id })
</td>
</tr>
}
</table>
Could you possibly give me a clue about where the error might be? I really don't understand 404 in this case.
EDIT 15:37 13.09.2016
With help from @StephenMuecke I learned what the cause of the problem was. He gave me a link which explains the problem in more detail: Dots in URL causes 404 with ASP.NET mvc and IIS
In fact my ID was sth like "12.34.56 00:00:00abcde". If you try to do it it causes IE error (404).
The answer is to change the ID into sth like "1234560000000abcde" which makes it ok for IE to proccess.
In my case this did the job:
//remove dots and colons from string
var datePart = DateTime.Today.ToString().Replace(".", "").Replace(":","");
//giving string properties and assigning for alias
var namePart = User.Identity.Name.ToString();
//removing @blablabl.ab - 12 signs, 13 is for the sake of correct indexing
var nameShort = namePart.Remove((namePart.Length)-13, 12);
//final ID concatenation
newRaport.raport_id = datePart + nameShort;
Thanks!