reuse kendo grid but change action - asp.net-mvc

I am trying to reuse a Kendo Grid on MVC by calling it on another view as a partial view. The Read property of the Ajax attribute of the grid is set to some method as follows:
#(Html.Kendo().Grid<MyModel>()
.Ajax()
.Read( read => read.Action("MyMethod", "MyController").Data("getData")))
However, I want that when this grid is called from another view, it changes the 'MyMethod' to 'SomeOtherMethod' and 'MyController' to 'SomeOtherController'. How can I do this?
Thanks!

I have a workaround for what you are trying to achieve. Please see below:
In all the views that you are trying to use the grid, set below TempData value:
#{TempData["ViewPath"] = #Html.ViewVirtualPath()}
Next you need create a generic master read controller action that will call another controller on the basis of the above tempdata value. So in your case in one view it will call 'MyMethod' of 'MyController' and in some other view it will call 'SomeOtherMethod' of 'SomeOtherController'.

Related

Recommended way to do the search and populating the grid in ASP.NET Core MVC 6

I am creating a project where I need to create a view with search textboxes and a dropdown which would be populated from the server. Same view also have a grid which would be populated based on the search criteria enter/selected by user.
I want to know what would be the design of the page in terms of showing both on same page. Should I create a partial view for the grid or Search panel or add both in the single view?
Note that dropdown list would need to be populated from the ViewModel. So what is the common practice in the situation. I am new to this I have done few pages but with lot of code, session and ViewBags and I think I am not following recommended practice.
Actually, the design for your web application is according to your requirement.
For example, if you want grid to work with other datasource or view, you could build it as a component, then if you want to use it, you could directly use this component to avoid write the same codes for multiple time.
If you want to use same grid to show in some other page, you could build it a partial view, then you could directly call this view to show something.

How to pass data from child action to layout in ASP.NET MVC

I have a view which uses a layout, and also executes a child action.
The layout calls a partial which needs some data from the view's child action. Is there some way to pass the data up from the child action to it's parent view's layout?
I have tried solving this using sections, but it appears that sections can only be rendered in layout views.
In the end, I had the child action stuff the required data into the HttpContext. This was then read out by the view containing the child action, which used it to render a section in the layout.
I was concerned that the order of processing of the views would not be predictable (i.e. that I couldn't be sure that the data would be written to the HttpContext before being read from it), but it turns out the order of processing is entirely predictable.
In your child action, you can set up the data as data-*** attributes and then use JavaScript (or jQuery) to read them back in your partial.
E.g. child action:
<div id="somedata" data-customerid="123" data-reference="xyz">
</div>
Then in your partial:
<script>
....
var customerId = $("#somedata").data("customerid");
var reference = $("#somedata").data("reference");
</script>
Hope this helps

How do I Change an MVC Actions' Associated View After using 'Add View...' Dialog Previously

If I use 'Add View' on a new Controller Action in Microsoft MVC, it will create a new view and I don't have to explicitly reference this view when calling it from the Controller:
return View();
However, what if at some time later I want to point my action to a different View? Since my Action is already implicitly bound to View created through the 'Add View' dialog, the only way I've found to override this implicit relationship is to explicitly identify the new view when returning from the Action:
return View("NewView");
Is there any way re-relate the "NewView" View to the Controller Action in the same way that the first View was implicitly bound through the 'Add View' dialog?
In order for the implicit
return View();
to work out of the box with a different named view you would have to change the name of your controller action.
If you are concerned with "magic strings" and would like to have a little better feeling that the view actually exists I suggest taking a look at T4MVC which will automatically generate strongly typed constants for your views.

ViewBag values accessibility in mvc

The controller in which I define a viewbag value, returns a partial view, which is the last view that is beinng returned.
This partial view is rendered to a JQuery dialog. After I close it, I return to the one before, which contains a form. In the form view (the one before the last one) I'm trying to access the viewbag value via JS function, and assign it to a hidden field in the form. So actually, I'm trying to get the viewbag value, not from the view to which I've sent the viewbag.
Is it a problem? Are viewbag values available only from the last view that has rendered?
I'm not sure if it's possible, but in any case you wouldn't normally want or need to access ViewBag outside of the view it belongs to. It seems like there would be a couple of better options.
populate the ViewBag value you need into a hidden field somewhere else in the DOM.
Since you're using JQuery dialog, consider using its callback function to pass the values you need back to the main form. See here for an example: jquery ui dialog box need to return value, when user presses button, but not working

ViewData not inheriting in partials

I was trying to use a shared partial view to render when a particular listing page has no data. I wanted to use ViewData to pass information from the page into my listing control, which would then conditionally render the NoData partial view using the ViewData values.
I would like to be able to specify them in the view markup, not in the controller action, but when I add them in the view the don't seem to inherit down into child partial views (like the Nodata partial view). However, specifying them in the ViewData values in the controller actions works fine, the data is available all the way down...
Does anyone know why it behaves this way?
When rendering a partial you can also pass the ViewData.
<% Html.RenderPartial("NoData", ViewData); %>
<%Html.RenderPartial("partialViewName", "viewData", "model"); %>
it is a best practice to do the decision inside the controller, if you have a scenario to make a decision inside the view, separate them and call them inside the controller conditionally

Resources