MvcContrib FluentHtml With Razor? - asp.net-mvc

I'm wondering does the latest release of MvcContrib work with the Razor view engine? I referenced the MvcContrib assembly and the FluentHtml assembly, then I added the required namespace to the ~/View/Web.config as suggested here by Darin. But still, no matter how much I try use the FluentHtml extensions in my views, it doesn't work. (nothing shows up in intellisense when I start with a dot after the html helper)
Am I missing anything?
P.S: this is the first time I use MvcContrib.

I wrote a short blog that post covers using FluentHtml with Razor.
Regarding intellisense, you will only get intellisense for FluentHtml methods on "#this." (not "#Html.") and it only list the strong-typed helpers on views that implement IViewModelContainer<T> (such as ModelWebViewPage<T>). Another possible issue is Resharper. There is a workaround for that.

I´m using mvccontrib and following darin answer it has to work. The steps I followed were:
copy-pasted in bin folder mvccontrib.dll and mvccontrib.fluent.dll
make a reference in references to those dll creating a reference for mvccontrib and mvccontrib.fluent.
two namespaces with the following names:
add namespace="MvcContrib"
add namespace="MvcContrib.FluentHtml"
in my controller I include using MvcContrib.Pagination;
finally I used in my view:
#using MvcContrib.UI.Pager
#using MvcContrib.Pagination
and rebuild.
hope it helps..

Related

How to include shieldui in asp.net mvc?

I'm new to shieldui js and asp.net mvc and I'm doing some examples for a train. I have no problem including the js in a html file but I'm struggling to include it in asp.net mvc. Lets say i have jquery and shield ui in Scripts folder. I did reference Shield.Mvc.UI but I still cant access them via #(Html.XXXX).
Lets say i want to create ShieldCalendar using View.cshtml. The example is:
<div class="container">
#(Html.ShieldCalendar()
.Name("calendar")
.HtmlAttribute("class", "calendar")
.Min(new DateTime(2009, 2, 23))
.Max(new DateTime(2039, 3, 1))
.Value(DateTime.Now)
.Footer(fb => fb.Enabled(true).FooterTemlpate("{0:dd.MM.yy}")))
I'm a beginner and I may miss something fundamental for asp.net MVC.
Later findings: it looks like some time ago there weren't html helper extension methods for that. Are you sure you have something like that?
Question reference: shield ui chart: generate series dynamically? (where op is saying that he wrote a custom html helper for .net mvc)
Html helpers reference: http://www.tutorialsteacher.com/mvc/html-helpers
Are you sure you have html helpers in Shield.Web.UI namespace? (see this question too: Referencing the Shield UI ASP.NET MVC javascript modules)
1st approach
You can create a bundle with those two javascript files for now.
Open the App_Start\BundleConfig.cs, there you will have a RegisterBundles method where you can create a bundle with those two files.
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jqueryand")
.Include("~/Scripts/jquery-3.1.1.js"))
.Include("~/Scripts/shieldui.js");
// i don't know exactly the name of the library
}
And in your Global.asax.cs, you need to register this bundle:
In Application_Start method, add the following line:
BundleConfig.RegisterBundles(BundleTable.Bundles);
Then in your _Layout or your more specific view, you'll have to add #Scripts.Render("~/bundles/jquery")
More about this approach here: https://www.asp.net/mvc/overview/performance/bundling-and-minification
And here:
How do I add BundleConfig.cs to my project? (probably this question is very useful)
2nd approach
Use something like RequireJs for .NET, which will bring some features to your application, but will also add some complexity.
See my answer for the question below:
RequireJS - ASP.NET MVC Bundle Script
About this part of your problem:
but I still cant access them via #(Html.XXXX).
Maybe your intellisense is broken, you can give it a try after you add the script to the page/layout and after you make sure that you have
#using Shield.Web.UI
for your page/layout.

MVCContrib Pager in Razor encodes HTML

I have following code
#Html.Pager((IPagination)Model.FoundUsers).Last("<span class=\"last\">&nbsp</span>").First("<span class=\"first\">&nbsp</span>").Next("<span class=\"next\">&nbsp</span>").Previous("<span class=\"prev\">&nbsp</span>")
But it renders encoded and shows <span class="next"> on page.
I tried to used Html.Raw as suggested in Problem with razor view and mvccontrib grid pagination or How to make a pager with MVCContrib and Razor?
but it still does not work for me.
What am I doing wrong ?
I would assume that you are using MvcContrib v2 with Mvc4 (or possibly Mvc3)?
Either manually download the newer libraries from http://mvccontrib.codeplex.com or use the Raw method. So instead of this:
#Html.Pager((IPagination)Model.FoundUsers).Last("<span class=\"last\">&nbsp</span>").First("<span class=\"first\">&nbsp</span>").Next("<span class=\"next\">&nbsp</span>").Previous("<span class=\"prev\">&nbsp</span>")
You would instead have:
#Html.Raw(Html.Pager((IPagination)Model.FoundUsers).Last("<span class=\"last\">&nbsp</span>").First("<span class=\"first\">&nbsp</span>").Next("<span class=\"next\">&nbsp</span>").Previous("<span class=\"prev\">&nbsp</span>"))
Perhaps it's because I am working in VB, but I had to get the appropriate string from the Pager object:
#Html.Raw(Html.Pager(Model).ToString)
I'm using MvcContrib 2.0.95 with MVC 3.

Share a razor declarative helper between multiple mvc web projects

Let's say I have 2 MVC web projects (web1 and web2) and 1 project containing shared views (common) (using the razorgenerator of David Ebbo)
web1 and web2 both have a test.cshtml file. Several blocks of code in both test.cshtml files are exactly the same.
I'm trying to find out if it's possible to share a declarative helper (#helper) between several cshtml files which are in DIFFERENT projects. So putting a cshtml file in my App_Code does not help (I would need to have 1 in each web project, which is obviously not what I want).
I know I could create a bunch of shared partial views in my 'common' project, but it seems kinda overhead to create 20 cshtml files that each contains a very small portion of html.
I know I can create a standard helper method (static string GenerateAPieceOfHtml(this HtmlHelper helper, ....)), but there I loose the ease of writing html as you can do it in a cshtml file.
For a short while I thought I bumped into an answer that would allow me to do it. But as I wrote in a comment, that code did not compile for me.
I hope my question is clear :)
[Update]
As csharpsi asks in a comment.. I did try out the code from the other post, but it did not spit out any HTML for me. Since I started to think that that answer should probably do the trick since it has 13 upvotes, I decided to give it a second try..
Again I didn't get any output, but then I tried it a little bit different.. and success!
I was trying this (which doesn't spit out any html on the page):
#{ new Test().DoSomething(Model); }
This is the version that DOES WORK:
#{
var html = new Test().DoSomething(Model);
#html
}
Other version that works:
#(new Test().DoSomething(Model))
What should I do with this question? Delete it? Write an answer myself?
Why are you trying to use razor helper for this anyway ? Razor helpers are one-particular-viewengine hack, your application shouldnt rely on them on many places (even amongst different websites). In this case, be sure to use standard MVC way - HTML helper. These you can easily share between websites, for example you can make your own class library full of them.

render usercontrol (cshtml) using #Html.Partial

I am getting my hands on MVC 3 and am confused that how do i use UserControls in my project.
I have created a usercontrol (cshtml) file called UserControl.cshtml and i am trying to render it Products.cshtml.
MyUserControl.cshtml resides in Shared folder.
In Products.cshtml:
<div>
#Html.Partial("MyUserControl.cshtml");
</div>
But i am getting this error. I dont know why is it trying to search .ascx file.:
The partial view 'MyUserControl.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Products/MyUserControl.cshtml.aspx
~/Views/Products/MyUserControl.cshtml.ascx
~/Views/Shared/MyUserControl.cshtml.aspx
~/Views/Shared/MyUserControl.cshtml.ascx
Is this the correct way to render usercontrol in mvc 3?
--Update--
This works.
#RenderPage("../Shared/MyUserControl.cshtml")
You do not need to specify the file extension, the view engine will handle that.
#Html.Partial("MyUserControl")
Phil haack has fantastic blog on the way to use Partial page.
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

Custom HTML Helpers in Razor & HTML Extension Methods

There are a lot of questions, here and everywhere, regarding the impossibility of using HtmlHelper extension methods (like ActionLink) in an #helper (razor) method.
Right now, in my project, I solved the problem passing as an extra parameter the current page (System.Web.Mvc.WebViewPage page) to the #helper, like in
#helper HelperFunction(SampleParameter sp, System.Web.Mvc.WebViewPage page)
and using it (trivially) as in
{
...
#page.Html.ActionLink("Title", "Action")
...
}
I would like to know if it's too stupid :-), and why. It would be better, of course, to have direct access to the current HtmlHelper of the page where the #helper is called, but, anyway... if this a good solution...
Andrea
I was curious why this didn't work, so I had a little look.
When you put code into the App_Code folder, it inherits from System.Web.WebPages.HelperPage and although this has a Html property, it's a System.Web.WebPages.HtmlHelper and not a System.Web.Mc.HtmlHelper, which is why you can't find things like ActionLink on it :(
I had a quick look around, and found this answer from Andrew Nurse:
Omar's got the right answer here, but
I wanted to add something (do feel
free to mark Omar's response as the
answer).
We were aware of this in v1 and
weren't able to get a great fix in the
product, but David Ebbo (an architect
on the ASP.Net team) posted a sample
of a Visual Studio Code Generator that
is basically a first exploration of
the kind of ideas we're looking at to
make this work properly:
http://blogs.msdn.com/b/davidebb/archive/2010/10/27/turn-your-razor-helpers-into-reusable-libraries.aspx
Try that out and see what you think!
Let David know if you have comments by
posting on his blog.
Unfortunately, it looks like you can't even put the Helper outside of App_Code into your Layout class or _ViewStart :(

Resources