Nesting elements using razor markup - asp.net-mvc

Am working on an Asp.Net MVC Web application, I am using a theme to replace the default theme and I would like to use #html.ActionLink() to generate a link. My problem is that the theme requires that I nest another element in the html link tag that will be automatically be generated. How do I nest elements using razor syntax?

I don't think that you can nest elements in razor syntax.
Solution for your problem would be to use #Url.Action() inside href property of a tag like this:

Related

ASP.NET MVC renders html tags & attributes in lowercase

It seems that both Asp.net MVC Core and Asp.net MVC 5.x render html tags & attributes in lowercase and this behavior breaks angular bindings. To give an example: in my view.cshtml I define Angular component with camel case binding:
<my-component [fooBar]="foo"></my-component>
which is later rendered as:
<my-component [foobar]="foo"></my-component>
As far as Angular bindings are case-sensitive, input-property fooBar inside component remains undefined. Is there any way to override this rendering behavior and render html as-is?
When I put this in a cshtml file:
<test-tag [testingThisThing]="123" anotherThingTEST="abc"></test-tag>
I get exactly the same in the HTML source. Are you sure that the element isn't being modified after loading? Are you looking at "View Source" (not "Inspect Element")?
That said, my test-tag isn't defined as anything anywhere - are you using Tag Helpers or something? MVC itself doesn't lowercase all attributes.
Looking at view source, it appears that it is being rendered by razor as written, but when the element actually appears in chrome, it is lower-cased. I can only assume this is a chrome "feature".

Html.ActionLink() vs. <a> tag

In ASP.NET MVC, for linking standard pages (the ones that does not need special parameters or query string), is there any situation where I should prefer Html.ActionLink() to a standard Link tag?
Thanks.
#Html.ActionLink is tied into the MVC routing definitions. It isn't just a helper for writing an anchor tag, it uses routing to determine what the href looks like and how it's structured.
By using ActionLink you insure all your links are rendered based on how your MVC routes are configured.
Routing is powerful and can prevent the need of having to have lots of query string variables or hidden fields to pass around data.
Use #Html.ActionLink, in the end the extra effort is negligible.
Its a matter of preference.
Html.ActionLink() is just a HTML Helper which will ultimately render a <a> tag.
I prefer using <a> tag.

html.beginform v old skool form tag

What is the benefit to using Html.BeginForm?
We're having some issues related to html helpers and razor generator view testing and I'm struggling to see the benefit which would stop us going back to old skool form tags.
Has anyone got an argument for or against either?
by old skool i mean:
<form action="#Url.Action('Blah')">
The Html.BeginForm is useful because it generates the url using the routes defined in the Global.asax. (or you can extend it with your own code)
Using the old tag is neither worst or best in my opinion. You simply have to generate your url manually or using the Url helper. In the end the html in the page will be the same
<form ....>
html
</form>
Html.BeginForm also implements IDisposable, meaning the form must be closed properly. It's a minor thing, perhaps, but not closing Html.BeginForm produces a run-time error, where an unclosed <form> tag does not.
no there is no difference , the form tag just use the routing to generate the url , so if you use #Url.Action you are good to go
there is even books use that way a plain old tag and a url helper to generate the route
ASP.NET MVC Website Programming is an example
Edit
**
starting from Mvc 4 there is no difference , prior to Mvc 4 , Mvc 3 for example require the Html.BeginForm to make the javascript unobtrusive validation to work

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.

Can I have form tag inside View in MVC?

Can we put form tag inside view in MVC. I need to keep report controller inside the form tag which is inside the view. It works fine. I am new to MVC, can anybody tell is it the correct way to work with.
Yes, you can put form tags in views. Forms are standard html tags, so it's not a problem. You can also put form tags with runat="server" if you want to render some server controls that require it.

Resources