문제

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

도움이 되었습니까?

해결책 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));
}

다른 팁

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 :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top