MVC Razor & AJAX paging - c#

Is there a way in which I can construct a MVC page such that, if required, I can pull the contents of it without the entire HTML frame. I.e. I want to be able to, if required, pull just the contents (for AJAX Paging) without the refreshing the entire page, but I want that to be possible too
#{
ViewBag.Title = "ViewDevice";
}
<h2>ViewDevice</h2>

You can use partial view to implement reusable part of a view and render it in any view you want like this #Html.Partial("_ViewDevice").
For more information on how to create a partial view see here and here.

Here is a great tutorial thats easy and quick:
http://www.joe-stevens.com/2011/05/30/asp-net-mvc-simple-server-side-ajax-paging-using-jquery/

check it out: https://github.com/kibiluzbad/Ifa
sample online demo: ifademo.apphb.com/
hope it helps

Related

How to create reusable control in ASP.NET MVC

How can/should I create some "custom control" in ASP.NET MVC 3? I have red about partial views, ViewUsersControl, Html.RenderAction, but I still don't know, which way is the proper MVC way for razor views.
If I need to render some ajax component to view, I can imagine to do it by partial view, but what if I want to render section with custom logic?
1) PartialViews
2) Custom Html helpers
3) Child Actions
Update ASP.NET Core:
2) Tag Helpers are preferred way over Custom Html helpers
3) View Components are used instead of Child Actions
You may use
#{Html.RenderPartial("YourCustomView",YourModel);}
For instance, In your Shared folder create a new View and name it "_MyCustomControl"
Then in the code write :
#model YourNameSpace.Models.YourModel
#{
Layout = null;
}
#*Here write your control's markup*#
Then in your Views where you want to use this "control" you add:
#{Html.RenderPartial("_MyCustomControl",new YourModel { args });}
If you get into trouble with RenderPartial check this out
In addition to all the other answers for this post, I can offer you the use of EditorFor and DisplayFor templates. These are useful when you want to easily render or edit a custom type. It'll handle validation nicely (which can get weird when using partials) and you can nest them recursively (again another feature that isn't obviously handy until you need it).
You can also use Html.RenderAction() or Html.Action() to call another controller action within a view and display the results in-line in the page. This is probably the closest to what you need as it allows you to render a partial, include code in the controller and it also allows for the passing of parameters.
Links to:
DisplayFor and EditorFor Templates
Action and RenderAction
As you have mentioned that you can use Partial Views.
Yes you can use Partial Views, which is the most effective and efficient way.
For Ajax rendering you can always use
#using (Ajax.BeginForm("Details", new { id = Model.Post.PostId }, new AjaxOptions
{
Few of the links you would like to see
Rendering partial view in ASP.Net MVC3 Razor using Ajax
Render Partial Views using JQuery in MVC3

Trying to use two layout pages on MVC4 and Razor

When I use #RenderBody on a view (not principal), I receive this message Error: The file "~/Views/Shared/_Sistema.cshtml" cannot be requested directly because it calls the "RenderBody" method.
I do not understand it as I am a novice in MVC.
What do I can do?
Thanks!
If you are using the Renderbody in _Sistema.cshtml file, then make it as a Layout page.
And add another partial page named like MyPartial.cshtml with Layout name as _Sistema.cshtml.
Renderbody is supposed to be in the master page only. i.e., Layout page.
So your _Sistema.cshtml page should only contains the following:
#RenderBody()
#RenderSection("scripts", required: false) #*----Optional---*#
Then your your new partial page MyPartial.cshtml should contain the following:
#{
Layout = "~/_Sistema.cshtml";
}
Then use your partial page in your view as follows:
#Html.Partial("MyPartial")
Hope it helps.
RenderBody is for masters only. This method renders markup of content pages that does not belong to any particular section. If your view calls RenderBody, two cases are possible:
Either this is a mistake and this view should not call it.
Or this view is a master, and you should instead use some other views inheriting layout from this master.
you just need to interact with _ViewStart.cshtml
and use if condition to specify the share mater page for each group of users.
for example user is admin then user _Layout.cshtm other wise use _Layout2.cshtml
use following code :
#{
if(User.Identity.Name.ToLower()=="admin") {
Layout = "~/Views/Shared/_Layout2.cshtml";
}
else {
Layout = "~/Views/Shared/_Layout.cshtml";
}
}
In my experience, I was struggling with this same issue for days. After further investigation, I found that when starting a new Umbraco site, I had the option to select templates. That resolved the RenderBody issue. When creating the layout page without the templates it doesn't see the Master page as the layout, hence not being able to call the RenderBody method. Using the templates automatically sets the Master page as the Layout allowing you to call the RenderBody. I hope this helps.

Getting the current URL within the View layer in ASP.net MVC

I've read some previous questions here and on other sites, but being new to ASP.net and MVC I'm having a bit of trouble understanding the information presented.
I want/need/was told to get the current URL of the page I'm on through the View layer, and use that information to apply an id to a li tag allowing for specific css. We've moved our left navigation bar from being embedded in every single page (done by a previous co-op) to putting the list in a partial view that I'm going to call on all of the required pages. Styling requirements for the site have a specific highlight on the left navigation a tag of the page the user is currently on.
Some of the examples I've read including using:
<%= Request.Url.PathAndQuery %>
Request.Url.ToString() or Request.Url.AbsoluteUri
var request = HttpContext.Current.Request
but I know not all of them can be used in the View layer. What would be the best approach? Are there any tutorials that I haven't been able to find yet that anyone could recommend?
It probably isn't the best idea, in my opinion, to use the URL for this.
Instead, a quick and easy way to achieve this is to use ViewContext.RouteData that will contain values for both the controller and action of the current request. It can be accessed from the view layer easily.
ViewContext.RouteData.Values["Controller"].ToString()
ViewContext.RouteData.Values["Action"].ToString()
So in your view you could do something like
<ul class="nav">
<li class="#(ViewContext.RouteData.Values["Controller"].ToString() == "ControllerName" ? "active" : "")">Foo</li>
</ul>
You could push it further to make it prettier, but you get the basic idea.
I was looking for a solution on this for asp.net 3.0 and found the following to give direct access to the URL path (what page you're currently loading/showing):
ViewContext.HttpContext.Request.Path
I'm using this in an if statement to decide dynamically between different View Imports, works very well.

Determine if View is being rendered as Partial

Is there a way to determine if a view is rendering as a partial?
I'm hoping to extend the reuse of a partial that I'm writing by catching this...and if necessary assigning the appropriate layout to the View.
At the moment I'm just rendering it in a div, but I could also see us using it as a modal and possible it's own page.
(the modal shouldn't require any change so no worries there)
EDIT:
To clear up what I'm asking.
I'm wondering if there is anyway to determine the difference between a view being rendered by...
/path/to/controller
and
Html.Partial("/path/to/view.cshtml")
Why not #if (Layout==null)?
Still I would recommend another view for the "own" page and set the layout there.
In your view (assuming Razor syntax):
#if(typeof(this) == Controller.PartialView)) //code
or
#if(this is Controller.PartialView) //code
Based on #Pheonixblade9 's response and the lack of other answers it doesn't appear like this is possible at the moment. I ended up just binding the Model of the View as bool and pass this value in when rendering the view/partial.

Can I populate a ContentPlaceHolder in a master page from within a Razor Partial View?

I'm using the typical built in view engine in mvc3 (is there a proper name for it?) for views and master pages and it's including a Razor partial view on the .aspx page. In the masterpage, there is a ContentPlaceHolder with an ID of "ScriptContent".
I want to be able to fill that ContentPlaceHolder from within the Razor partial view but I don't think this is possible. Does anyone know if it is possible and how I would go about doing that?
I already tried rendering it in the partial like so, but that didn't work.
#section ScriptContent {
... content ...
}
It would be very difficult, so much so that I'd recommend finding another way :(. I wish it was easier, but these are the complexities of integrating a new view engine into an existing legacy system.
To give you a head start if you really want to try it: You'd probably need to create a custom base class inheriting from WebViewPage for your Razor content pages, override some of the methods (honestly I'm not too familiar with that aspect so you'd need to debug to follow the pipeline) so that instead of treating the Layout property as the path to a Layout page, you treat it as a Master page. Then you'd need to instantiate the master page and somehow convert the Sections (which were transformed into calls to DefineSection by the Razor parser, and should be stored in a Dictionary somewhere on the base class) in to Content controls and stuff them in the Master Page.
If I haven't boggled your mind by this point, you may just be able to pull this off, but to be honest, I'd avoid it.
P.S. We refer to the older view engine as "ASPX", based on its file extension ;).

Categories

Resources