Asp.net MVC Magic strings - eliminate - asp.net

If I have a strongly typed view e.g. Inherits="System.Web.Mvc.ViewPage>" %>
And i want to output the list of items in the model, when I use "Add view" within visual studio i get the following html code generated below.
Is there anyway to eliminate the Magic string "Version" when outputting the table column headers?
<table>
<tr>
<th>
Version
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.Version) %>
</td>
<tr/>
<% } %>
</table>

What would you like to have instead?
You could use resource files so they are translated or something..
Or if it has to be the name of the item.Version variable you can probably look into some reflection.

I think you'll have to use reflection. This question looks like it will help you:
How do you get a C# property name as a string with reflection?

If you have your controllers and views in the web project, just add Resource Files (.resx) to App_GlobalResources (or App_LocalResources relative to the view, though I'm not a fan of that) and then reference the resources from yoru views.
I personally create a "Resources" project that contains a bunch of Resource Files (.resx). Each resx is set to public (dropdown at the top of the designer). This way they can be accessed from Views in the web project or controllers/application services in my other projects.
Having said that, unless sharing copy or translation is a major concern to you, it's ok to leave the copy in the views as a "magic string".

Related

How to Link to a file in ASP.NET MVC 4 using Razor?

I'm making a web app using ASP.NET MVC 4.
It lets people upload files.
I want to list all the files from a user with a link to open/download each file.
I have the virtual path to each file (e.g. :~/Folder/file.txt), how can I generate the link with razor?
I tried with #Href but it doesn't render anything, same thing with #Url.Content.
I tried also without using razor but I don't think it's a good way...
Your help would be welcome! Thanks!
I don't know why #Href didn't work but it's the way to do it!
Here is a sample code:
#foreach (var s in Model)
{
<tr>
<td>#s.Id.ToString()</td>
<td>#s.Title</td>
<td>
#if (s.FilePath!= null && s.FilePath!= "")
{
link
}
</td>
</tr>
}

Combine code with info from databound item in ASP.NET markup code blocks

Man, I never really learnt all the embedded code blocks and stuff you can use in ASP.NET. What I'm trying to do is the following:
I have a repeater
It renders a table
In each row, I need to add a data-bind attribute (yes, for Knockout) containing some text and the rowindex.
More specifically, I want to render:
<table>
<tr data-bind="with:myItems()[0]">
...
</tr>
<tr data-bind="with:myItems()[1]">
...
</tr>
<tr data-bind="with:myItems()[2]">
...
</tr>
</table>
I've tried:
data-bind="<%# String.Format("myItems()[{0}]", Container.ItemIndex) %>"
But that doesn't work (data-bind="<%# Container.ItemIndex %> will however. So I'm trying to combine code with information from the databound item.
I know there is a foreach binding in Knockout, but I can't use it because:
I want/need my HTML to be constructed server-side initially
There's other, specific javascript that needs the HTML to exist already so I can't let Knockout populate the table
I'm using an ASP.NET Repeater, which doesn't mix well with Knockout's templates.
I also know, I could just do this in code-behind (with <tr runat="server" ... >) but I'm trying to put all my layout and javascript in markup and js files, not in C# code.
So, can I, in some way, add code in my markup to combine text I choose, with info from the current databound item?
Bummer, apparently, the answer is dead simple, and it didn't work the first time because of another mistake I made:
<tr data-bind="with: myItems()[<%# Container.ItemIndex %>]">
I put more info on my Blog and a working example on GitHub.

How can I write code in the code-behind file for my ASP.NET 2.0 page that hides table rows without changing the IDs of the child elements?

I'm stumped by a seemingly simple problem. In my ASP.NET page, I have a table which has a few rows that need to be shown or hidden conditionally from the back end. Sounds simple, right?
What I tried is something like this in the front-end code:
<table>
<tr>
<td id="demorow1">
<p>This row always shows up!</p>
</td>
</tr>
<tr id="conditionalrow" runat="server">
<td id="formoptionsrow">
<!-- This row contains a number of form elements that should only SOMETIMES be shown, as determined by the back-end code. -->
</td>
</tr>
</table>
And in the code-behind file I just do this to hide the code:
conditionalrow.Style["display"] = "none";
This makes the row disappear as intended. I don't mind that it's just invisible, it won't hurt anything. However, this has the side-effect of making several HTML form elements inside of conditionalrow gain ASP.NET's convoluted IDs and NAMEs. This throws off a lot of Javascript functions related to the form that I don't have time to change or rework right now. I need to be able to hide the form (or remove it from the code entirely) from the code behind file, but without changing the IDs and NAMEs of child elements.
I know there's some kind of setting in the newer versions of ASP.NET that allows you to override ASP.NET's ID reassignment. Unfortunately, I'm stuck with ASP.NET 2.0 and don't have the option of using anything newer for this project. What do you recommend?
Instead of making the row a server side control, use a code block to give it an appropriate CSS class.
<tr class="<%:VisibilityClass%>">
Where, in your code behind you have a VisibilityClass string property that return the CSS class name:
public string VisibilityClass
{
get
{
if(shouldBeVisible)
return "visible";
return "hidden";
}
}
You can also use functions if a property is not appropriate.
can you not do a conditionalrow.Visible = false;

asp.net mvc webforms like gridview

is there a grid for asp.net mvc that acts just like the asp.net webforms datagrid
the behavior that I need is to pass to the grid a DataTable without specifying the amount of columns
Have a look at MvcContrib. They provide a Grid HTML helper, which is very powerful.
I have extended that Grid in my free library, etcetera.Mvc, added AJAX paging, sorting and some more nice options.
In my opinion you should not be using any of the server controls in MVC.
You will need to use a table and iterate through thr required objects, MVC allows much more control over the HTML which meens that you have to do a little more work.
I think you best look at the NerdDinner example.
http://nerddinner.codeplex.com/
And here is an example,
<table>
<% foreach (var category in Model) { %>
<tr>
<td><%= Html.Encode(category.data) %></td>
<tr>
<% } %>
</table>

asp.net - Use ascx as a layout template

This is my layout template (ascx without code behind)
<%# Control Language="C#" AutoEventWireup="true" Inherits="ws.helpers.LayoutUC" %>
<div>blah blah blah</div>
<ws:Panel runat="server" ID="left"></ws:Panel>
<ws:Panel runat="server" ID="main"></ws:Panel>
<ws:Panel runat="server" ID="right"></ws:Panel>
Modules will be added into ws:Panel later.
I also allow my user create their own ascx file to custom their page layout. And because of this i do a string replace all dangerous part like script tag (runat="server"), all asp.net html tag, <%, <%#, <#.... from their custom.
Im not worry about XSS, so dont comment on it, and ask why?
I want know your thinking about this. Is is safe? Is it scalable? Is it standard or a bad way?
Have a look at the INaminingContainer Interface http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx.
<asp:YourControl>
<LeftColumn>
<asp:Literal ID="literal1" runat="server" Text="User created literal" />
</LeftColumn>
</asp:YourControl>
In the .ascx from the users, they register your control and insert asp.net code into properties. In the 'YourControl' class you create placeholders and insert the markup set to a specific property into these placeholders. (e.g. everything between <LeftColumn> and </LeftColumn> will the inserted into
<asp:Placeholder ID="PlaceholderLeftColumn" runat="server"/>
Edit: I summed some of the TemplateContainer issue up and posted it here: http://www.tomot.de/en-us/article/2/asp.net/how-to-create-an-asp.net-control-that-behaves-as-a-template-container-to-nest-content-via-markup
You are allowing user-uploaded content; this is inherently unsafe and there are whole books dedicated to best practices. Given that you are doing it anyway, as long as you make sure you scrub the input, is it scalable? You are allowing creation of user-uploaded files on your site. How many will there be? How many users? What about load-balancing? This solution will not scale for many users, files, or servers.
It sounds like you are trying to create a simple CMS. Why not use one that exists currently, or adopt parts of an open source solution?

Resources