Question

I have the default route defined as

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

I want to generate a url like this in html -

<form action="/app/Request/c35d4520-ba0b-452f-837b-a00046f40647 method="post"> 

But if I code Razor page like -

@using (Html.BeginForm("Default", "Request", FormMethod.Post, new { id = ViewBag.AppId }))

The rendered html is -

<form action="/app/Request" id="c35d4520-ba0b-452f-837b-a00046f40647" method="post"> 

How can I force the razor generate the url as controller/action/ID format?

Thanks

Was it helpful?

Solution 3

The problem has been solved by a function -

protected ActionResult RedirectToAppAction(string controllerName)
{
    return this.Redirect(string.Format("/App/{0}/{1}", controllerName, this.Id));
}

OTHER TIPS

Try using Html.BeginRouteForm

@using (Html.BeginRouteForm("Default", new { controller = "foo", action = "bar" }, FormMethod.Post, new { id="foo", enctype="multipart/form-data", accept_charset="utf-8" }))
{
}

the problem is in the way you arrange the parameters in the Html.BeginForm call. According to the parameters you feed in, you are currently calling

Html.BeginForm(actionName, controllerName, formMethod, htmlAttributes)

Therefore, new { id = ViewBag.AppId } is treated as htmlAttributes. And that is why the id is renedered as an attribute in the form tag.

Instead, you should swap the positions of method and the id as below

Html.BeginForm("Default", "Request", new { id = ViewBag.AppId }, FormMethod.Post))

Let me know if it works for you :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top