How do I distinguish between these 2 routes in my area registration?
These are the last 2 routes in that area registration.
The first route is when the view is loaded. Works fine.
First route loads a form which then get posted to the same controller but different action.
I never get OK from controller. It might not hitting controller due to routing issue.
What I am missing?
context.MapRoute(
"Load",
"app/respond/{Id}",
new { controller = "Controller1", action = "Index" }
);
context.MapRoute(
"Update",
"app/respond/{action}",
new { controller = "Controller1", action = "Update" }
);
This is how the form looks:
@using (Html.BeginForm("Update", "Respond", FormMethod.Post, new { id = "frmUpdate" }))
{
//all form fields go here
}
This is how the posting is done:
$('#frmUpdate').submit(function () {
//verify all field values
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
if (result.s == 'OK')
alert("Success! Response updated");
else
alert("Sorry! Update failed.");//this is what I get
}
});
return false;
});
My Controller:
[HttpPost]
public ActionResult Update(MyModel model)
{
return Json(new { s = "OK", m = "Hi from controller" });
}
You can’t distinguish between those 2 routes because they follow absolutely the same url pattern
app/respond/something. You didn’t put any constraint onsomethingso the first route will always match.If you want to have any chance of the routing system being able of distinguishing you need to use a constraint, for example let’s say that an
{id}must contain only numbers:Now when you request
app/respond/123the Index action will be called and when you invokeapp/respond/FooBartheFooBaraction will be called. And if you requestapp/respondtheUpdateaction will be invoked.