When does a variable need an # and when does it not? - asp.net-mvc

I am trying to enhance the default Shared/Error.cshtml that comes with the standard MVC template in VS2013. The model is System.Web.Mvc.HandleErrorInfo and I added the following:
#foreach (var key in Model.Exception.Data.Keys)
<li>#key : #Model.Exception.Data[key]</li>
It gives an error in the key in #Model.Exception.Data[key]: The name 'key' does not exist in the current context.
Why and how do I get each value from the dictionary Data?

try this:
#foreach (var key in Model.Exception.Data.Keys)
{
<li>#key : #Model.Exception.Data[key]</li>
}
razor need "{" to know that the code is inside the foreach loop

Related

How to bind a model to a table in SAPUI5

im trying to bind an entitySet from a oData-Service to a list.
My code looks like this:
var list = oView.byId("list");
var requestModel = new sap.ui.model.json.JSONModel()
.attachRequestCompleted(function(data) {
var model = new sap.ui.model.json.JSONModel();
model.setData(data.getSource());
list.setModel(model);
});
requestModel.loadData("/sap/opu/odata/sap/XXX_SRV/detailSet?$filter=XXX eq 'XXX'");
My service returns a array of detail-Objects as expected but i can't seem to find a way to bind them to the list.
Thanks
I finally found a solution:
At first i had to create a dummy Path in my list like this:
<Table class="sapUiResponsiveMargin" items="{/dummy}" id="table" width="auto">
When you can bind the url directly to the table:
var url = "/XXX?$filter=XXX eq '" + XXX + "'";
var table = oView.byId("table");
table.bindItems({
path: url,
template: table.getBindingInfo("items").template
});
To get started with the ODataModel try this guide. In general it is very easy.
Instantiate the model like described in the guide.
Set the model to the view.
Make use of the binding syntax in XML views to trigger a request to load your entities.

How does partial views work?

In a MVC master view I have this:
#Html.Partial("_CreateOrEdit", Model)
and the partial view _CreateOrEdit contains this code block:
#{
var item = Model;
.
.
.
string favouriteId = "fav_" + item.Id + "_comment";
}
Despite ensuring that item.Id > 0 I get a null reference error in pointing at the declaration of favouriteId. The dots mean there are many other similar declarations all with non null reference.
When I replace item.Id by #item.Id, rendering the partial views fail in the master view.
What am I doing wrong? Can someone point to a complete tutorial on code blocks please?
Thanks
Another '#' cannot be used since the statement is already within the razor code block.
In order to avoid the object reference exception, use the below code
string favouriteId = string.Format("{0}_{1}_{2}", "fav", item.Id, "comment");

loop through model in mvc razor code behind

I am working on MVC4 App, and I am stuck at one point, I tried using google to help me, but without success. This might be more simple then I think, but coming from web forms and shifting to mvc is "painful" sometime.
I am trying to loop through the model I have and get the values stored in that model. I tried few approaches but I am getting an error everytime. This is what I have:
var modelAgentFilter = from s in _aa.Agents
where s.COUNTER == Convert.ToInt32(AgentID)
select s;
if (modelAgentFilter != null)
{
ViewBag.FirstName = // Get FirstName object here
}
Thanks in advance for your comments.
Laziale
EDIT:
I did include for loop like this:
if (modelAgentFilter != null)
{
foreach (var property in modelAgentFilter)
{
string test = property.ADDRESS;
}
}
But when the compiler will reach the foreach step I am getting this error: "LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression."
I can get to the properties of the var model using that foreach look but as soon as the compiler will try to loop the model that error pops up.
Thanks again
LINQ to Entities does not recognize any methods. You can't use even ToString() in LINQ expression. You need first convert your value and than add it in LINQ.
In your example you need to do something like following:
var _agentID = int.Parse(AgentID);
var modelAgentFilter = from s in _aa.Agents
where s.COUNTER == _agentID
select s;

How To access properties in velocity Templete?

I have an properties file in one location (/home/XXXX) and want one of its property acessed in velocity templete in location (/irb/dev4/ccm_wa/cb532/RBAPI-XXX/RBAPI/BA/tools/src/com/gen/tools/build/j2ee/ejb). how do we access them??
I need the below foreach loop to be executed only if the above accessed property returns true
#if( **PROPERTY IS TRUE** )
#foreach ($databaseGroup in ${root.getAllDatabaseGroups().values()})
#foreach ($index in ${databaseGroup.getIndices()})
<resource-ref>
<res-ref-name>${databaseGroup.getResRefPrefix()}${databaseGroup.getTxDs(${index})}</res-ref-name>
<resource-name>${databaseGroup.getResRefPrefix()}${databaseGroup.getTxDs(${index})}</resource-name>
</resource-ref>
In your Template class read the properties file into an Properties object and put this to the context. i would suggest Apache Commons PropertiesyConfiguration for that:
PropertiesConfiguration props = new PropertiesConfiguration("/home/XXXX");
contxt.put("props", props);
in your template file you can now check your properties:
#if($props.getBoolean(propertyName))

Umbraco 5 Very Basic - How to load an entity in a macro (or actually using a HiveId)?

I've created my first custom Umbraco 5.1 solution. At this point I have a content item ("homepage") with a custom template which has a custom partial macro on it. Now how do I load an entity using the Umbraco helper? I've tried adding several HiveId constructions using a Uri, however I keep getting the same error:
Parameter 'other' must be of type Guid to convert to a Guid CLR type, but it is 'Uri', with value: xxx
Macro partial:
#inherits PartialViewMacroPage
#using Umbraco.Cms.Web
#using Umbraco.Cms.Web.Macros
#using Umbraco.Framework
#{
//All these fail with the same error message...
//Based on name:
var p = Umbraco.GetContentById(
new HiveId(
new Uri("content://Homepage")));
//Based on path
var p = Umbraco.GetContentById(
new HiveId(
new Uri("content://p__nhibernate/v__guid/5a4abe489a2e47858bd2a0580180b683")));
//With custom Hive provider (I've added this using a custom tree/section and products show up, so the hive provider works)
var p = Umbraco.GetContentById(
new HiveId(
new Uri("custom://products/1")));
}
Why are you creating a Uri?
The HiveId accepts a string parameter which you can use instead. So does Umbraco.GetContentById(string id)
I am Umbraco 5 certified and we never used the Uri overload of the HiveId constructor.
var p = Umbraco.GetContentById("yourStringHiveIdHere"); //(string overload) or
var p = Umbraco.GetContentById(new HiveId("yourSringHiveIdHere")); // (HiveId overload)
Also where are you getting your HiveId from?

Resources