Asp.net mvc html.DisplayFor syntax in linq/lambda - c#

I'm currently studying asp.net mvc and I just started, I decided to move away from web forms to mvc.
I understand the basics of linq and lambdas but I would just like to know or get a good explanation about this particular syntax.
#model IEnumerable<CodeplexMvcMusicStore.Models.Album>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Genre.Name)
</td>
I would just like to know what is the meaning of modelItem => item.Genre.Name
My knowledge on this is that modelItem gets the value item.Genre.Name and then it is passed method Html.DisplayFor().
I'm also curious about how do I write the same code without using lambda.
Correct me if I'm wrong I would just like to know the meaning of the code and how it is read.

Read this: Why All The Lambdas? : Good article explaining the use of Lambdas.
The lambda expressions (of type Expression) allow a view author to use strongly typed code, while giving an HTML helper all the data it needs to do the job.

You can write
#model IEnumerable<CodeplexMvcMusicStore.Models.Album>
#foreach (var item in Model) {
<tr>
<td>
#Html.Raw(item.Genre.Name)
</td>
Or
#model IEnumerable<CodeplexMvcMusicStore.Models.Album>
#foreach (var item in Model) {
<tr>
<td>
#item.Genre.Name
</td>

Related

Error:A local variable named 'Model' cannot be declared in this scope

I m returing a List to the View. So The Model is IEnumerable. It works fine in foreach loop to get the employees. But when I use this Model inside the loop it give me the error:
A local variable named 'Model' cannot be declared in this scope because it would give a different meaning to 'Model', which is already used in a 'parent or current' scope to denote something else.
Inside the loop when I use other name other than Model it works fine.
View
#model IEnumerable
#foreach (tbEmployee emp in Model)
{
<tr>
<td>
#Html.DisplayFor(Model => emp.empID)
</td>
<td>
#Html.DisplayFor(Model => emp.empName)
</td>
<td>
#Html.DisplayFor(Model => emp.empAge)
</td>
<td>
#Html.DisplayFor(Model => emp.empStatus)
</td>
<td>
#Html.ActionLink("Edit", "Edit", "Employee")
#Html.ActionLink("Update", "Update", "Employee")
#Html.ActionLink("Delete", "Delete", "Employee")
</td>
</tr>
}
But When I write like, it works fine
#foreach (tbEmployee emp in Model)
{
<tr>
<td>
#Html.DisplayFor(x => emp.empID) //where this x get data from
</td>
<td>
#Html.DisplayFor(x => emp.empName)
</td>
<td>
#Html.DisplayFor(x => emp.empAge)
</td>
<td>
#Html.DisplayFor(x => emp.empStatus)
</td>
<td>
#Html.ActionLink("Edit", "Edit", "Employee")
#Html.ActionLink("Update", "Update", "Employee")
#Html.ActionLink("Delete", "Delete", "Employee")
</td>
</tr>
}
First off, this error message has been removed in more recent versions of C#; the design team decided that it created more confusion than prevented errors.
The issue is that the name Model is here used to mean two things: whatever property or variable is used as the collection of the foreach, and as a formal parameter of a lambda. Moreover, the two usages overlap. That is, the Model that is used in the foreach is directly outside the usages of Model as a formal inside the foreach. Basically the compiler here is saying "you are creating a situation where you're possibly using the same name to mean two completely different things right next to each other" and that is a very confusing and bug prone situation to be in.
(Having two lambdas beside each other with the same names for the formal parameters is not illegal because they do not overlap. If one lambda was in another, that would give a similar error.)
This error message is well-intentioned but regrettably, the compiler does a poor job of explaining the problem to the user. I did some work to improve that error message in... 2011 maybe? Or thereabouts. But the improvements were marginal at best.
As you've discovered, the correct fix is stop using the same name to mean two completely different things in the same scope.

ASP.Net MVC Razor Sum and Count functions

Is it possible to use "Sum" within Razor, so you can sum up what has been interated through on the view. ie. my view is like this:
#model IEnumerable<cb2.ViewModels.ResultsVM>
...
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Qualified)
</td>
...
}
I then want to sum up all of the Qualified in at the bottom of the screen similar to this:
#Model.Qualified.Sum()
But I get the error:
'System.Collections.Generic.IEnumerable<cb2.ViewModels.ResultsVM>' does not contain a definition for 'Qualified'
I thought it would have been easy in Qazor to simply use Sum or Count on a model?
thanks, Mark
I think you want:
#Model.Sum(i => i.Qualified)
Qualified is a property of the items within the model, not the model itself.
Remember that Model is an IEnumerable<cb2.ViewModels.ResultsVM>, it does not contain a property for Qualified, but each item within the collection does. So you can call Sum directly on the collection and specify the property that you want to sum, namely Qualified...
#Model.Sum(x => x.Qualified)

ASP.NET MVC: Dynamically rendering controls based on user's access

In my application, I have two roles: Administrators and Users. Administrators can assign Users to allow them to perform specific functions to a specific object.
Since this goes beyond the scope of what the SimpleMembership Roles can provide, I have a static helper method that checks whether a user has access to a specific function:
public static class SecurityCheck
{
public static bool UserHasAccess(int objectId, string functionName)
{
// Decorates the security provider -- gets logged in User ID and calls to a repository to query the database
// ...
}
}
Which I can then use in my views to determine whether or not a specific function should be rendered for that user based on the object's ID:
#foreach (var item in Model.FooData)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Notes)
</td>
<td>
#Html.ActionLink("View Data", "View", new { #id = item.Id })
#if (SecurityCheck.UserHasAccess(item.id, "Edit Data"))
{
#Html.ActionLink("Edit Data", "Edit", new {#id = item.Id})
}
#if (SecurityCheck.UserHasAccess(item.id, "Delete"))
{
#Html.ActionLink("Delete", "Delete", new {#id = item.Id})
}
</td>
</tr>
}
I have to believe there is a better way to do this, since each individual call to the static method involves a separate round-trip to the database, but I am stuck on where the best place would be to put the code. Some thoughts I have considered:
Adding methods to my ViewModels to pass a list of functions to a repository, returning a list of the functions the user can perform for each object. Thinking about this further, I'm not even sure this is possible, as ugly as it would be.
Keep the ViewModel dumb, and have my application layer service fetch the available functions. This would involve adding additional properties to my domain model objects, which I am not crazy about.
Create a separate service that can be called from the controller that can populate the function list to the ViewModel. This would involve having multiple services injected into each controller -- also not crazy about this.
I'm leaning towards #2., but I still feel like I am overlooking what would be a more solid implementation. Has anyone dealt with something similar to this before?
I think each ViewModel "knows" what can be done with it, isn't it? So we can make implicit explicit. The ViewModel can explicitly have properties such as CanEdit, CanDelete, etc.
The UI should not care why some operations are allowed or not, it simply checks these properties in a way:
#if (item.CanEdit)
{
#Html.ActionLink("Edit Data", "Edit", new {#id = item.Id})
}
You can even come up with a helper that takes another boolean as a parameter to decide whether the control should be rendered (or enabled) or not, but it is minor:
#Html.SecureActionLink(item.CanEdit, "Edit Data", "Edit", new {#id = item.Id})
The idea is that it is not the responsibility of the UI to know how to figure out whether something is permitted due to some business rules or not.
But it is definitely UI's responsibility to know how and what to render in one ViewModel is not Editable or another is ReadOnly (different things can have different states).
Also, since we are talking about DDD I would advice against modeling CRUD operations. In the end of the day DDD is about Ubiquitous Language, and "Create, Update, Delete" is hardly a language business really speaks.
So you will end up with more precise and meaningful properties/operations in your models, such as CanAccept (for order screens) or `CanMakeRefund" (for payments).
You resolve/set these properties when you build up your ViewModel and apply security context to it.
Hope it helps.
Maybe you need to use SimpleMembership roles:
Assigning Roles with MVC SimpleMembership
In the standard MVC membership you can just use something like:
Roles.AddUserToRole(model.UserName, "Admin");
And in you View e.g.:
if (ViewContext.HttpContext.User.IsInRole("Admin"))

Check for null value of model property inside partial view

I am working on a asp.net mvc 3 application and I've made several partial views each one responsible for rendering of specific logic. Inside one of my views I use properties which can be null, but I don't want to pass null to the #Html.DisplayFor() and write something more user friendly to the user to know that these fields are not missing, they just don't have nothing assigned to them yet.
So I try this :
<tr>
<td>
#if (!string.IsNullOrEmpty(Model[0][0].FieldValue))
{
#Html.DisplayFor(Model => Model[0][0].FieldValue)
}
</td>
<td>
#Html.DisplayFor(Model => Model[1][0].FieldValue)
</td>
</tr>
I don't have else clause because writing the if statement results in getting both Model => in the DisplayFor marked with red and the following message :
A local variable named 'Model' can not be declared in this scope
because it would give a different meaning to 'Model' which is already
used in a 'parent or current' scope to denote something.
Basically I think I understand what this error means however I don't know how to check for null properly in this situation.
The error message is caused by the redefinition of the Model variable. Try
#Html.DisplayFor(x => x[0][0].FieldValue)
You might find this SO question useful to understand the "=>" thingie.

I want to understand the lambda expression in #Html.DisplayFor(modelItem => item.FirstName)

I’m fairly new at C# and MVC and have used lambdas on certain occasions, such as for anonymous methods and on LINQ.
Usually I see lambda expressions that look something like this:
(x => x.Name), (x => { Console.WriteLine(x))
I understand that lambda = "goes to". I have never seen a lambda expression where the left parameter is not used.
I don’t know how to translate this lambda expression though
#Html.DisplayFor(modelItem => item.FirstName)
Can anyone shed some light on this one for me? Shouldn’t this be
(modelItem => modelItem.FirstName)?
I got this from Microsoft's Introduction to ASP.NET MVC tutorial.
A lambda expression is a way to write an anonymous function, i.e. a function without a name. What you have on the left side of the "arrow" are the function parameters, and what you have on the right side are the function body. Thus, (x => x.Name) logically translates to something like string Function(Data x) { return x.Name } the types string and Data will obviously vary and be derived from the context.
The absence of the left-side parameter translates into a function without parameters, so that this (() => someVariable) logically translates to this: string Function() { return someVariable; }
At this point you might start wondering, where someVariable comes from, it's not a function parameter and it is not defined in the function. You would be correct, a function like this would never compile. However the lambda function like this is perfectly fine, as it allows outer-scope variables be lifted and used this way. (Internally a wrapper class is created where the variables that are used in the lambda expression become fields.)
Now let's see what model => item.FirstName means. Logically it would translate to string Function(Model model) { return item.FirstName; }. Basically this is a function with a parameter, but this parameter is not used.
And now, the last bit of the information. Although lambda expressions represent functions in the end, sometimes they are created not with the purpose of actually being executed (although potentially they can). A lambda expression can be represented in the form of an expression tree. This means that instead of executing it it can be parsed.
In this particular case the MVC engine does not run the function that the lambda expression represents. Instead the expression is parsed so that MVC engine knows what html to emit for this particular line. If your data item does not come from your model object directly, the left part of the lambda expression does not matter.
i think it's about the foreach loop. example:
#foreach(var item in model)
{
<td>
#html.displayfor(model => item.firstName) </td>
</td>
}
var item needs to be used because each item in the sequence is an anonymous type.
model => item.firstName means (input parameter) => expression. you can't use the input parameter because we store the current "item" in item.
It is using a parameterless lambada. See this question
Basically DisplayFor doesn't use the lambda function parameter model (it could be anything I'd say use _ or ()) and just uses the lambda function within the for loop to use displayfor against it. DisplayFor requires a lambda function.
I also struggled a lot to understand the codes generated by Visual Studio. Instead of providing a general explanation about lambda expression, I would like to put a context using ASP.NET MVC framework.
Suppose we prepared a Model class (e.g. Destination) with 2 attributes: City and ProvinceCode.
public class Destination
{
public string City { get; set; }
public string ProvinceCode { get; set; }
}
After generating the Controller and View, we should get the generated codes by Visual Studio as mentioned. Yet, the generated codes are somewhat hard to understand, especially for the data rows
#Html.DisplayFor(modelItem => item.City)
I just guess that the MVC team should think that Html helper class should be consistently used in the cshtml file. Thus, they tried to use tricks to pass the C# compiler. In this case, modelItem is not even used as an input parameter of this lambda expression. We can't use () as the type is NOT correct. That is why, if we replace model or any model object, the lambda expression works.
To be honest, I would like to rewrite the generated codes in a more readable form. Instead of using the Html helper class, we can simply render the correct output as follows:
#foreach (var item in Model) {
<tr>
<td>
#* Code Generated by Visual Studio. modelItem is a dummy param *#
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
#* A better way - simply get rid of Html helper class *#
#item.ProvinceCode
</td>
</tr>
}

Categories