Alternative to Html.EditorFor in MVC 1 - c#

I am following the mvcMusicStore tutorial and it is very good but the problem is that it asks me to include a html helper known as "Html.EditorFor". This html helper is not in MVC 1 which is what I am using. What else can I use to go around this? This helper is suppose to allow me to link the Edit View to an edit template know as Album which I have created. This is the coding which is included in the tutorial:
<%:
Html.EditorFor(model => model.Album,
new { Artists = Model.Artists, Genres = Model.Genres})
%>
Please look at the MvcMusicStore Tutorial from page 40 onwards to help you understand the situation. This can be download from Microsoft's MVC website.

Templated helpers were introduced in ASP.NET MVC 2. Html.RenderPartial is probably the closest you could get in ASP.NET MVC 1.0. It is strongly recommended to migrate to at least ASP.NET MVC 2.0, assuming ASp.NET MVC 3.0 is the current stable production release.

Related

Use Repeater or HTML table in ASP.NET MVC?

I'm going to take a few courses in ASP.NET Core MVC and for the frontend HTML stuff I am finding a varied number of examples on how a task should be completed.
The task in question is to display a list of products which contains an image, title, short description, price and View More or Add button for each product.
In webforms, I used to grab a repeater and then used the RowBound event to carry out other functionality.
When I first started to learn ASP.NET MVC, there were no repeaters or gridviews, but recently I have noticed these in the toolbox. The problem I found with these controls in webforms was it added a lot of additional Javascript so when it came to adding CSP we couldn't as these controls broke the page when certain events were triggered.
Fast forward to today and ASP.NET Core MVC can use a similar approach use a HTML table and maybe dataTable to do the same thing (I think from my research) using JQuery, Ajax etc.
My question is, are the available repeater/gridview controls in ASP.NET Core MVC any better/same as webforms? Should I invest in learning in other HTML/jQuery third party controls instead?
Going a little further forward, I would like whichever is the preferred method to also be inline with ASP.NET Core 6 so there isn't a huge learning curve so appreciate any answers someone can provide as to what they have done for controls such as repeater/gridview in ASP.NET Core MVC as I'm pretty confused what should be used.
The fact that in modern asp.net applications is that we are using loops to render items. For instance, if you want to render a list you will have something like this:
#model IEnumerable<ViewComponentSample.Models.TodoItem>
<ul>
#foreach (var todo in Model)
{
<li>#todo.Name</li>
}
</ul>
Where you are rendering all items in the model defined at the top of the page. Which is being passed to the view from the controller.
And yes you can all kind of loops such as #for, #foreach, #while, and #do while based on your situation.
This allows you to render all kinds of items like rendering table rows, bootstrap cards, etc.
And even to make it enhance it furthermore you can create a partial and render items dynamically and improve the code readability and maintainability.
Partials are just a razor markup file that renders HTML output within another markup file's rendering output
#foreach (var product in Model.Products)
{
<partial name="_ProductPartial" model="product" />
}
Read further at Microsoft docs: Create view component
Read further at Microsoft docs: Render partials in a loop
Read further at Microsoft docs: Looping in asp.net
You can easily list your products using Asp.Net Core Razor.
In your .cshtml file you can use such a razor code.
<ul>
#foreach (var product in products) // 'products' is your Model
{
<li>#product.Name</<li>>
<li>#product.CreationDate</li>
<li>#product.Description</li>
<li><img src="~/img/#product.Picture" alt="ProductImage_#product.Name"/></li>
}
</ul>
Here is an introduction about razor:
Razor is a markup syntax for embedding .NET based code into webpages.
The Razor syntax consists of Razor markup, C#, and HTML. Files
containing Razor generally have a .cshtml file extension. Razor is
also found in Razor component files (.razor). Razor syntax is similar
to the templating engines of various JavaScript single-page
application (SPA) frameworks, such as Angular, React, VueJs, and
Svelte.
As the introduction says, You can use c# code in razor page. For Example, You can use Looping like #for, #foreach, #while, and #do while in razor.
More details you can refer to Razor syntax reference for ASP.NET Core.

How to organize multilingual URLs in ASP.Net Core Razor Pages?

ASP.NET Core 2.2.0
I'm looking for language-based urls in ASP.NET Core. Found a lot of examples online for .NET Core MVC, but can't get it working in Razor Pages.
What I want to achieve is:
domain.com/informatie -> link to the 'information' page in default language (Dutch for me)
domain.com/en/information -> link to same the same page in English
Therefor I need 2 features:
Recognize the language in the url, or set default language when language-tag is not provided
Translate the names of my Pagemodels
I will write a custom method for translating the content.
Best fitting example
I found 2 examples that do almost the same I want, but they are using MVC:
ASP.NET Core: Simple localization and language based URL-s
Localized routing using ASP.NET Core MVC 2
The second one looks great, I tried to rewrite it for Razor Pages, but got stuck on the LocalizationController.cs (since I'm not using a controller, but a Pagemodel) CultureActionLinkTagHelper.cs (I use the asp-page attribute).
Is there anybody who has fixed this in Razor Pages and who would like to share the code? Or anybody who can help me through?
You need to build your application considering globalization/localization.
Here is the official documentation from Microsoft.
You may find this tutorial for Developing Multicultural Web Application helpful, it is for DotNetCore 2.1 Razor Pages, but it is valid for 2.2 as well.
Additionally, her is a sample project for localizing razor pages in GitHub

ASP.NET Core 2.1. Razor UI Class Library in MVC View

I'm currently working on an ASP.NET Core 2.1. RC1 Final (May 2018) demo application. I try to use the new Razor UI Library in my ASP.NET Core MVC application.
Thus, in my Solution I have 2 Projects:
ASP.NET Core MVC application
Razor Class Library
The structure of my Razor Class Library is very simple:
RazorClassLib1
Areas
MyFeature
Pages: Page1.cshtml
The cshtml of Page1.chtml looks like that:
#page
#model RazorClassLib1.MyFeature.Pages.Page1Model
#{
Layout = "_Layout";
}
<h1>Hello From the Razor ClassLib1</h1>
Now I want to use this Page1.cshtml Razor component in my MVC View. This feature would be useful to organize and reuse razor pages within my application.
Unfortunately I have no idea how to achieve this. I was following this example:
http://www.talkingdotnet.com/asp-net-core-2-1-razor-ui-as-class-library/
though it seems to work only for a razor page project, but I would need it in my MVC projct.
Do you know how to use a Razor Class Library in ASP.NET MVC Core 2.1. Views?
Thank you very much.
https://gunnarpeipman.com/aspnet/razor-class-library-mvc/
explains how to set it up.
There are some things to know:
Controllers must have AreaAttribute
Web application must have area route defined.
I think there work arounds using the areas, but I am researching this myself. will update the answer when I have figured it out.
Below link also explains how to make the area name dynamic:
https://blog.tech-fellow.net/2018/11/11/razor-ui-class-library-with-dynamic-area-name/

percentage tags in cshtml

Following along on a tutorial in MVC, would you tell me the alternative to this in a ASPX page
<div>
<%=ViewData["CurrentTime"]%>
</div>
In a cshtml page, the default view type in MVC 4 on VS2013. When I try the above, the literal meaning is displayed. i.e. ViewData["CurrentTime"] instead of the value DateTime.now
When you using a cshtml. You are working with Razor engine.
In Razor engine, You need to use # like
#ViewData["CurrentTime"]
<%=ViewData["CurrentTime"]%> is a ASP.Net engine code
There are two engines in MVC: Razor and ASP.NET WebForms.
.cshtml filles are the files using Razor engine and this is a default engine of ASP.NET MVC from the third version.
The syntax you are using would work with the old WebForms engine.
So you should write:
#ViewData["CurrentTime"]
in case you use ViewData or
#ViewBag.CurrentTime
in case you use ViewBag which is more common when using Razor view engine and in general newer version of ASP.NET MVC .

UrlHelper with ASP .Net Webforms

I need to be able to set up links in my webpages dynamically in ASP .Net Webforms. I know that you can set up routes dynamically in ASP .Net.
I want to generate Urls too from the RouteCollection in a similar way to the UrlHelper in ASP .Net MVC. So far I couldn't find anything that says if you can use the UrlHelper from ASP .Net MVC with Webforms.
You might be interested in the FriendlyUrls Nuget package which Scott Hanselman recently blogged about, and introduced in this blog post.
Code example:
<a href='<%# FriendlyUrl.Href("~/WebForms/Edit", Item.TouristAttractionId ) %>'>Edit</a>

Categories