What is the difference between RenderPartialExtensions.RenderPartial and DisplayExtensions.DisplayFor - asp.net

I want to have an "dynamic" table ascx:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<% foreach (var element in Model.Elements) { %>
<%= Html.DisplayFor(m => element) %>
<% } %>
with model:
public class TableViewModel<ElementType> {
public List<ElementType> Elements {get;set;}
}
first strangely enough I cannot write something like this in ascx directives:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl>" %>
then the second problem - I cannot use DisplayFor function extension, because compiler cannot deduce HtmlHelper<> generic param, because the ascx model is dynamic. That is why I had to replace the:
<%= Html.DisplayFor(m => element) %>
with the:
<% RenderPartialExtensions.RenderPartial(Html, "Table", element); %>
So my questions are am I doing something wrong, or if I'm not what is the difference between DisplayFor and RenderPartial in terms of performance?
Thanks in advance.

May be you could try to use just Display?
RenderPartial just renders partial view. DisplayFor uses UIHint or DisplayTemplate from Shared views for rendering.

Related

Backbone.js/Rails model with children collections of itself

Suppose this:
Model Foo:
-> id
-> name
-> description
-> children[]
Where children is a collection of Foos. Every Foo can have zero or more children, all having the same basic structure as their parent.
What would be the proper way to do the view/template for this in Backbone.js? I am building on top of a Rails app if that makes any difference.
I ended up using something similar to what is described here: Run Template in Template (recursion) within underscore.js template engine but I am using CoffeeScript so it is slightly different:
The view:
# foo.js.coffee
class MyApp.Views.Foo extends Backbone.View
template: JST['foo']
inititalize: ->
#init stuff
render: =>
templateFn = #template
$(#el).html(#template(model: #model, templateFn: templateFn))
#
The #model will have children inside that share the structure of their parent.
The template:
# foo.jst.eco
<%# other output stuff here %>
<%# there is a 'components' attribute that contains the children %>
<% if #model.get('components') : %>
<%# recursive output of components %>
<% for e in #model.get('components') : %>
<% foo = new MyApp.Models.Foo() %>
<% foo.set(e) %>
<%# note the - instead of = below... in my case the JSON contains HTML entities %>
<%- #templateFn(model: component, templateFn: #templateFn) %>
<% end %>
<% end %>

asp.net mvc2 dropdownlistfor

I want to create a dropdownlist in my asp.net MVC2 view and I am following code:
foreach (var whiteout in Model)
{
%>
<tr>
<td>
<%= whiteout.Field.NiceName%>
<% Html.DropDownListFor("anyname", Model); %>
<%
}
}
%>
but I am getting error that second parameter is not correct. Second parameter is a list. Here is how Model is declared at the top of partial view:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<EnviroTracker.Entities.Whiteout>>" %>
Please suggest how to fix this?
A DropDownListFor helper takes a SelectList as second argument and a lambda expression to a simple property as first:
<%= Html.DropDownListFor(
x => x.SomeProperty,
new SelectList(Model.SomeList, "ValueProperty", "TextProperty")
) %>
If you want to use the weakly typed DropDownList helper you could manually specify the name of the property it will be bound to but the second argument should still be a SelectList:
<%= Html.DropDownList(
"SomeProperty",
new SelectList(Model.SomeList, "ValueProperty", "TextProperty")
) %>

ASP.net MVC 2 - Using textbox with linq

I am using LINQ and sending a linq object to the view and trying to show it in a textbox like this
<%=Html.TextBox("petname",Model.PetName) %>
But I am getting error, please suggest how can I show (PetRecord.PetName) which is my linq entity
Thanks
Raj
What does your Inherits property look like:
<%# Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
It should inherit whatever model you're sending to the view:
<%# Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<PetRecord>" %>
And you should be sending the following to your view from the controller:
public ActionResult ShowPet(int petId)
{
PetRecord pet = repository.GetPetById(petId);
return View(pet);
}

Html.EditorFor in an MVCContrib PortableArea

I am trying to use Html.EditorFor in a Portable Area (i.e. so create and edit use the same form), but it doesn't seem to working (fine when it is not in the Portable Area though). In the page, I just have:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MySite.Models.DocumentType>" %>
...
<%= Html.EditorFor(model => model, new { }) %>
Then in the 'EditorTemplates' sub folder I have my editor code
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MySite.Models.DocumentType>" %>
...
However, it doesn't seem to be using it to render the editor.

How can i call class from aspx file?

i have a class which is in App_Code/Kerbooo.cs i want to call that class's method from aspx file (not from code behind) is it possible? if it is, how can i do? thank you very much already now.
If the method is static, then the following should work within the aspx page:
<% Kerbooo.Method1(...) %>
If the method is not static, then you'll need an instance of Kerbooo:
<%
var kerbooo = new Kerbooo();
kerbooo.Method1(...)
%>
First, import the namespace that your code in App_Code uses:
<%# Import Namespace="MyNamespace" %>
If your code isn't in a namespace yet, it's a good idea to put it in one.
Next, you can call your code either with <% code; %> or <%= code %>, depending on whether you want to write the results to the output stream or not.
Data binding, as in <%# %>, requires a little extra work, as do expressions in <%$ %>
You can use <% %> and put your code in between (if you want to write stuff out <%= %> is a short cut for response.write but you need to do this outside of the <% %>
<%
var bob = new Kerbooo();
..do stuff with class
%>
you can mix and match (this does lead to spaghetti code so be carefull)
e.g looping
<table>
<%
var bob = new Kerbooo();
foreach(var thing in bob.GetThings())
{
%>
<tr><td><%=thing.StuffToWrite%><td></tr>
<%}%>
</table>
And your method should be public if your aspx does not inherit from a class in codebehind

Resources