3

i ve got a weird problem. My view :

@{
   ViewBag.Title = "Index";
}

<h2>Index</h2>
@using(Html.BeginForm())
{
     <input type="submit"  value="asds"/>
}
@Html.Action("Index2")

My Controller:

public class DefaultController : Controller
{
    //
    // GET: /Default1/

    [HttpPost]
    public ActionResult Index(string t)
    {
        return View();
    }


    public ActionResult Index()
    {
        return View();
    }

    //
    // GET: /Default1/

    [HttpPost]

    public ActionResult Index2(string t)
    {
        return PartialView("Index");
    }

            [ChildActionOnly()]
    public ActionResult Index2()
    {
        return PartialView();
    }
}

When i click on a button [HttpPost]Index(string t) is executed, wich is fine. But after that [HttpPost]Index2(string t) is excuted and thats really weird to me because i ve posted data for Index action not for Index2. My logic tells me that [ChildActionOnly()]ActionResult Index2() instead of HttpPost one.

Why is this happening? How can override this behaviour without renaming [HttpPost]Index2 action?

1 Answer 1

2

That's the default behavior. It is by design. If you cannot change the POST Index2 action name you could write a custom action name selector that will force the usage of the GET Index2 action even if the current request is a POST request:

public class PreferGetChildActionForPostAttribute : ActionNameSelectorAttribute
{
    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        if (string.Equals("post", controllerContext.HttpContext.Request.RequestType, StringComparison.OrdinalIgnoreCase))
        {
            if (methodInfo.CustomAttributes.Where(x => x.AttributeType == typeof(HttpPostAttribute)).Any())
            {
                return false;
            }
        }
        return controllerContext.IsChildAction;
    }
}

and then decorate your two actions with it:

[HttpPost]
[PreferGetChildActionForPost]
public ActionResult Index2(string t)
{
    return PartialView("Index");
}

[ChildActionOnly]
[PreferGetChildActionForPost]
public ActionResult Index2()
{
    return PartialView();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I think it could help. But I realy cant understand why this behaviour is not used as default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.