mvc2 dropdownlist dynamic parameters - asp.net-mvc-2

<% string disabled="new {disabled='disabled'}"; %>
<%= Html.DropDownList("clientId", someObject, disabled)%>
In the above code I want the text disabled to be replaced by what ever value I set that string to. When I check the HTML source on the page, I see that new {disabled='disabled'} has been added as a new item in the dropdown list instead of a property. How do I fix this?

The third parameter of DropDownList helper must be an object that contains the HTML attributes or object of type IDictionary<string, object>.
This is the proper solution:
<% var disabled = new { disabled = "disabled" }; %>

Related

How to pass parameter to a java function in JSP scriptlet?

I want to pass the value of assembler.area.id, sent by controller to the java function getIsAreaWaitTimeActiveByAreaId(), inside scriptlet. I am not sure how to do so. getIsAreaWaitTimeActiveByAreaId() takes in Long.
<c:set var="isWaitTimeActive" value="${assembler.area.id}" scope="page"/>
<%#page import="com.ihc.wtrack.service.impl.ReportServiceImpl"%>
<%
ReportServiceImpl reportController = new ReportServiceImpl();
boolean isActive = reportController.getIsAreaWaitTimeActiveByAreaId(isWaitTimeActive);
%>
Thanks.

Bind a form field in Play 2.0 with a constant value?

I have a scala form with several fields.The fields in the form map to the member variables of a Java class. I want to bind one of the fields(say userId) with a value (I dont want the user to enter values for this field. Instead i want to pass this as a parameter to the scala template). However, i was unable to manually bind a form field. Any help is highly appreciated.
See the sample below for easier understanding :
`#(itemForm: Form[Item], user: User)
#import helper._
#main("Item list") {
#if(user != null) {
#form(routes.Application.newItem()) {
#itemForm("userId") = #user.id /**I want to bind the userId form field */
#inputText(itemForm("title"))
#inputText(itemForm("description"))
#inputText(itemForm("price"))
<input type="submit" value="Create">
}
}
}`
In this case it would be better to pass it as action's argument (remember to modify routes declaration)
#form(routes.Application.newItem(user.id)){
....
you can also just use common html
<input type="hidden" name="userId" value="#user.id" />
edit:
Validation in action.Note: it doesn't make sense to display errors on the page next to hidden field, so you do not need placeholders for error messages. It's up to you to pass VALID value into the hidden field. Displaying validation errors to user who can not change the value of hidden field is bad conception.
public static Result newItem(){
Form<ItemModel> itemForm = form(ItemModel.class).bindFromRequest();
if (itemForm.hasErrors(){
return badRequest(newItemView.render(itemForm));
}
itemForm.get().save();
return ok("Your new item is saved...");
}

How to get the value of lambda expression in asp .net mvc2?

I have a action result method where it gives values from the database and Iam passsing this values to the view and the values will be populated in the textboxes but i have radio button to be selected depending on the value of the database,example if the value is male then radiobutton as to be selected else radiobutton female aas to be selected.I have written the code
<% if(model=>model.Gender) {%>
<%= Html.RadioButtonFor(model => model.Gender, "Male", "Checked")%> Male
<%} else { %>
<%= Html.RadioButtonFor(model => model.Gender, "Female", "Checked")%> Female<%} %>
but Iam getting a error like "Cannot convert lambda expression to type 'bool' because it is not a delegate type" .Please tel me how to check the value of Gender and make selection accordingly.
Assuming you've got access to the model at that point, you don't need the lambda expression at all:
// Or whatever
<% if (model.Gender == Gender.Male) {%>
However, I think it's unlikely that this is how you're meant to use RadioButtonFor. I'd expect it to be able to pick up the right button to check automatically.

public definition of GetEnumerator in asp.net mvc missing?

Should I manually create a definition for GetEnumerator? Seems like it should know...
I get the following error:
foreach statement cannot operate on
variables of type
'MvcAppNorthwind.Models.Product'
because
'MvcAppNorthwind.Models.Product' does
not contain a public definition for
'GetEnumerator'
Line 9: <h2><%: ViewData["Message"] %></h2>
Line 10: <ul>
Line 11: <% foreach (MvcAppNorthwind.Models.Product p in ViewData.Model) { %>
Line 12: <li><%= p.ProductName %></li>
Line 13: <% } %>
In my controller I have this code:
NorthwindDataContext db = new NorthwindDataContext();
ViewData["Message"] = "Welcome to ASP.NET MVC!";
var products = from p in db.Products
select p;
return View(products);
I changed the declaration in my view like this and now it works:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcAppNorthwind.Models.Product>>" %>
But if you want to display or use data from several models in the same view? How do you do it then?
Change the type of "products" from var to IEnumerable<MvcAppNorthwind.Models.Product> and make sure your cast reflects the same.
In answer to your last question, you could assign objects to a dictionary item in ViewData OR better yet you could create a View Model that contains all of the data that you need for the view. That way you have better separation of concerns by having a model that is specific for your view.

Display empty textbox using Html.TextBoxFor on a not-null property in an EF entity

I am using Entity Framework (v4) entities. I have an entity called Car with a Year property of type integer. The Year property does not allow NULL. I have the following in my Create view:
<%= Html.TextBoxFor(model => model.Year) %>
I am required to return a new Car object (due to other requirements) in my HttpGet Create action in the CarController.
Currently, a zero is displayed in the Year textbox because the Year property does not allow NULL. I would like to display an empty textbox in the Create view. How do I do this?
Use Html Attributes Overload. In razor it would be:
#Html.TextBoxFor(model => model.Year, new { Value = "" })
Try this instead:
Html.TextBox("Year", "")
If you are using the strongly typed TextAreaFor helper, then there is no direct way to set a default value. The point of the strongly typed helper is that it binds the text area to a model property and gets the value from there. If you want a default value, then putting in the model would achieve that. You can also just switch to the non-strongly typed TextArea helper. It gives you more a bit more flexibility for cases like this:
#{
var defaultValue= "";
}
#Html.TextArea("Model.Description", defaultValue, new { Value = "added text", #class = "form-control", #placeholder = "Write a description", #rows = 5 })
Try this is you are trying to append to your field or want to modify an existing field with an empty TextBoxFor.
Html.TextBoxFor(model => model.Year, Model.Year="")