ilay zeidman
ilay zeidman

Reputation: 2814

How can I know in the controller which partial view made a callback

I have two partial views that has the same model, I am using devexpress callbacks. My question is if I can know in my controller function which partial view made the callback and then render it? Or I need to duplicate the function and just render in each function the correct partial view?

Upvotes: 0

Views: 270

Answers (1)

Michael
Michael

Reputation: 1473

Your view returns to you only that you pass into it. So, you should pass your view-name into the hidden field container, as an example, and then read this value from the server side. Someone already answered how to pass view name, I will try to extend this answer.

First of all, you need to create some view-path parsing extension

public static class IViewExtensions
{
    public static string GetViewName(this IView view)
    {
        string viewUrl = String.Empty;
        if (view is BuildManagerCompiledView)
        {
            viewUrl = ((BuildManagerCompiledView)view).ViewPath;
        }
        else
        {
            throw new InvalidOperationException("Buld manager is not defined!");
        }

        string viewFileName = viewUrl.Substring(viewUrl.LastIndexOf('/'));
        string viewFileNameWithoutExtension = Path.GetFileNameWithoutExtension(viewFileName);
        return (viewFileNameWithoutExtension);
    }
}

Then pass your each view-names into the form container

@using ViewExtensionNamespace;
<input type="hidden" id="ViewName" name="ViewName" value="@Html.ViewContext.View.GetViewName()" />

And read it from the server side

 name = Request.Params["ViewName"];

Upvotes: 1

Related Questions