asp.net mvc2 dropdownlistfor - asp.net

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")
) %>

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 %>

What is the difference between RenderPartialExtensions.RenderPartial and DisplayExtensions.DisplayFor

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.

Why can't I use an iteration variable in a LoginView?

I am building a .NET MVC app that has a page with a list of delete buttons, one for each item in a list. The problem I'm having is that the foreach variable "item" is not visible inside the LoginView, which results in the following error:
Compiler Error Message: CS0103: The name 'item' does not exist in the current context
Below is a simplified version of the view. The error occurs at the "new {id=item.Id}" in the LoggedInTemplate - the reference to "item" in the ActionLink works fine:
<% foreach (var item in Model) { %>
<%= Html.ActionLink("Item", "Details", new { id = item.Id })%>
<asp:LoginView runat="server">
<LoggedInTemplate>
<% using( Html.BeginForm( "Delete", "Items", new {id=item.Id}, FormMethod.Post))
{ %>
<input type="submit" value="Delete" runat="server" />
<% } %>
</LoggedInTemplate>
</asp:LoginView>
<% } %>
To clarify the problem is not that the Model has not been successfully passed to the View. The Model is visible from both inside and outside the LoginView. The foreach loop as no problem in iterating through the items in the Model (which is a List). The problem is that the iteration variable "item" is not accessible from within the LoginView - though the original Model is.
Is there any way to pass "item" through to the LoginView's templates? Or is building LoginViews within a foreach loops the wrong way of doing things?
Is there a scoping rule that prevents using local variables within controls - perhaps because the control is rendered at a different time to the main page?
With ASP.NET MVC you really shouldn't use user/custom controls, so if you omit the <asp:LoginView/> and write a line of code to check if the user is authenticated, you are good to go.
Instead of your current code:
<asp:LoginView runat="server">
<LoggedInTemplate>
<div>Show this to authenticated users only</div>
</LoggedInTemplate>
</asp:LoginView>
Just use an if-statement and the value of Request.IsAuthenticated:
<% if (Request.IsAuthenticated) { %>
<div>Show this to authenticated users only</div>
<% } %>
Are you passing the Model to the view and are you also inheriting from the model within that view?
So if this is a View then in your C# code you need to return the list of items like return View(listofitems);
If this is a partial view then <% Html.RenderPartial("MyPartial", listofitems) %>
And in the view you need to
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IQueryable<ListOfItems>>" %>
If all that is in place then it should work no probs.
<% foreach (var item in Model) { %>
<%= Html.ActionLink("Item", "Details", new { id = item.Id })%>
<%= if( Request.IsAuthenticated ) {
using( Html.BeginForm( "Delete", "Items", new {id=item.Id}, FormMethod.Post))
{ %>
<input type="submit" value="Delete" runat="server" />
}
} %>
<% } %>
There is no need to use the LoginView, its not really giving you anything. Use something like the above instead.
Alternatively, you can move the decision of whether to show the delete option for the specific item into the controller, so instead of doing if( Request.IsAuthenticated ) you would do if( item.ShowDelete ) ... assuming item's type is a view model. Another option is to use an extension method for the same, item.ShowDelete(). I prefer the earlier, because there might be logic associated to deciding whether to show delete for a given item, so its better to not have it in the controller or a related logic.

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

Validating dynamically created fields in ASP.NET MVC

I have the following form in an ASP.NET MVC view:
<%= Html.ValidationSummary() %>
<% var fields = ViewData.Model; %>
<% using (Html.BeginForm("Dynamic", "Candidate")) { %>
<% foreach (var field in fields) { %>
<label for="<%= field.FieldName %>"><%= field.FieldName %></label>
<%= Html.TextBox(field.FieldName, field.Value, new { #class = "short" }) %>
<% } %>
<a class="button green" onclick="$('form').submit(); return false;">Submit</a>
<% } %>
I have a single controller action that loads this form as well as accepts the post, it looks like this:
public ActionResult Dynamic() {
var fields = DataProvider.Candidates.GetAllDynamicFields();
if (Request.HttpMethod == "POST") {
fields.ForEach(f => f.Value = Request[f.FieldName]);
var validation = DataProvider.Candidates.SaveDynamicFields(fields);
if (validation.IsValid)
return RedirectToAction("Index");
ViewData.ModelState.AddErrorsFromValidationResult(validation);
}
return View(fields);
}
My problem is that if any of the validators fail (i.e. the validation object contains errors) then I get an error on view rendering because ViewData.ModelState doesn't contain any keys. Where am I going wrong here? Any clues?
Figured it out. ViewData.ModelState is populated by the params in the response object. So with a dynamically created form you don't know exactly what was passed in the post. So I just recreate my ModelState on the fly:
fields.ForEach(f => ViewData.ModelState.Add(f.FieldName ...
And then we're all good...when the validation is run on the view it can find all the keys in the ModelState and no exceptions...works like a charm.
Asp.Net C# MVC Dynamic Forms (Changing Dom structure and getting data on the server)

Resources