开发者

finding the action name inside post method of an ascx - asp.net mvc

I have a partial control formy.ascx that I am using on a lot of pages that contains a form.

When I click submit on the partial control, following function handles the form submission.

[ActionName("FormyTemp"), AcceptVerbs(HttpVerbs.Post)]
public ActionResult FormyTemp(FormCollection result)

Now, I need to know what page i.e. action I was on when this form was submitted.

I tried passing (string)ViewContext.RouteData.Values["action"] as one of the form parameters, but it's telling Formy as the action when that is just a partial control. How should I go about doing this? I can't just look at the URL be开发者_如何学Pythoncause some of my URL's are like domain.com/actionName and some of them are like domain.com/controllerName/actionName.

Also, please don't tell me to use RenderPartial...I need to use RenderAction


From my understanding, you need to know the action of the Parent form, not the individual RenderAction that rendered the partial. In that case you can use the following:

ViewContext.ParentActionViewContext.RouteData.Values["action"];


When rendering the action:

<% Html.RenderAction("Formy", new RouteValueDictionary { 
    { "ParentAction", ViewContext.RouteData.Values["action"] } 
}); %>

and the Formy action:

[ChildActionOnly]
public ActionResult Formy(string parentAction)
{
    // the parentAction parameter will have the desired value,
    // so you can pass it to the view model Formy.ascx is strongly
    // typed to and include it as hidden field in the form. When
    // the form is later submitted the value will be passed to FormyTemp
    return View();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜