How to hide default zero from amount field in mvc3 [closed] - asp.net-mvc-3

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to show placeholder value in text box if Model Cost value is '0', but it's showing default '0'. What is incorrect in the following code?
<div class="leftflotdiv">
#if (Model.SkillSets[j].Cost > 0)
{
#Html.TextBoxFor(x => x.SkillSets[j].Cost, new { #class = "input_newmainbg", placeholder = "Cost per hour", Value = Model.SkillSets[j].Cost })
}
else
{
#Model.SkillSets[j].Cost
#Html.TextBoxFor(x => x.SkillSets[j].Cost, new { #class = "input_newmainbg", placeholder = "Cost per hour" })
}
</div>

Try this. Make the name of the field according to your model field to bind the input field to the model property.
<input type="text" name='SkillSets[#j].Cost' placeholder="Cost per hour" class = "input_newmainbg" />

Related

Converting DateTime? value to Date in MVC 3 view [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an MVC3 application with a model that contains a DeployedDate property of the type DateTime?. In my view I want to only show the date in the text box but not the time.
<%: Html.EditorFor(model => model.DeployedDate) %>
<%: Html.ValidationMessageFor(model => model.DeployedDate) %>
If you are open to modify Model, You can use DisplayFormatAttribute. You can specify ApplyFormatInEditMode
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? DeployedDate{ get; set; }

Add checkboxes dynamically to controlgroup [duplicate]

This question already has answers here:
Dynamic controlgroup and checkboxes unstyled
(6 answers)
Closed 8 years ago.
<div data-role="fieldcontain">
<div id="listado" data-role="listview">
<fieldset data-role="controlgroup" id="equiposHechos">
</fieldset>
</div>
</div>
and my javascript is
var identificador=4
$( "#recibo" ).on( "pageinit", function( event ) {
for(i=0;i<=identificador;i++){
$("#equiposHechos").append('<input type="checkbox" name="calificarEquipo'+i+' id="calificarEquipo'+i+'"/>'+'<label for="calificarEquipo'+i+'">Equipo Numero'+(i+1)+'</label>');
}
})
I want to add checkboxes dynamically ... but I get no styles as I do to recharge them or that they charge me as they should be when they are static
If you're using jQuery Mobile 1.3.2 or below, you need first to enhance/style markup of checkbox or radio buttons using .checkboxradio() and then re-style controlgroup by calling .controlgroup("refresh").
var identificador = 4;
for (i = 0; i <= identificador; i++) {
$('<label for="foo' + i + '">Foo' + i + '<input type="checkbox" id="foo' + i + '"/></label>').appendTo("#equiposHechos");
}
/* widgets enhancement */
$("[type=checkbox]").checkboxradio();
$("#equiposHechos").controlgroup("refresh");
Demo
You have to explicitely ask jquery mobile to restyle them:
Check this:
Dynamic controlgroup and checkboxes unstyled
$("input[type='checkbox']").checkboxradio("refresh");

Razor textbox, "value" property not showing [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Watermark for Textbox in MVC3
Im switching my HTML code from basic html to use html helpers and Html.TextBoxFor...
This is my old code
<input type="text" disabled="disabled" value="Phone" name="Phone" id="cust-cellphone" class="tonedDown" />
And this is the new version
#Html.TextBoxFor(x => x.Customer.Phone_Personal, new { #class = "text-adr-fld tonedDown", #value = "Phone", #disabled = "disabled" })
Everything is working fine, except that the Value property is gone. What I mean is that it's not showing as a default predefined value inside the textbox.
I read that you can use Placeholder, but it doesnt seem to work with IE9 (which is a requirement from my client).
So the question is, how do I add input Value property to the TextboxFor method that works with IE9?
Edit:
There might be a possible workaround somehow.
My original purpose with this is to display a "placeholder like value", ie a default value which shows what the user is supposed to type in the textbox. E.g. the textbox that handles phonenumber should display "Phone", until the user clicks and enters a value.
Does anyone know another way of doing this (except "placeholder" and "value")?
u can set the default value to your model, and then your piece of code will work, else you can try using
#Html.TextBox("Phone_Personal", "Phone", new { #class = "text-adr-fld tonedDown", #disabled = "disabled" }).
If u want to use TextBoxFor, then using
$(document).ready(function(){
$("#Phone_Personal").val("Phone"); // replace Phone_Personal with the ID of the textbox
});
I think you don't pass the model to View method :
return View(model);
Or,the model is null.

Validation of DropDownListFor is not working in MVC3?

Validation is Working on Other Input type text element but not working on DropDownListFor
Class Purchase Input Property Code
[Required]
public string LedgerId { get; set; }
Class View Model Code
PurchaseViewModel purchaseVM = new PurchaseViewModel
{
// PurchaseInput=purchaseInput,
Ledger = uw.LedgerRepository.Get().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.LedgerName }),
};
View
<div class="column">
<div class="labelField">
#Html.LabelFor(model => model.PurchaseInput.LedgerId, "Party")
</div>
<div class="ItemField">
#Html.DropDownListFor(model => model.PurchaseInput.LedgerId, new SelectList(Model.Ledger, "Value", "Text"))
#Html.ValidationMessageFor(model => model.PurchaseInput.LedgerId)
</div>
</div>
On the face of it, it seems that you do not have an empty item in your select list. The validation will only trigger if the user selects a dropdown item with string length of zero. If you examine the Html source can you see the validation attributes on the dropdown ( depending on whether you are using unobtrusive validation or not)?
Yes, there are problems with validation of DropDownListFor. look at this link. They get validation data manually from metadata - http://forums.asp.net/t/1649193.aspx
Although this is a workaround, at least it fires some sort of validation. Try:
#Html.DropDownListFor(model => model.PurchaseInput.LedgerId, new SelectList(Model.Ledger, "Value", "Text"), new { #class = "required" })

How do you validate individual collection items in an ASP.NET MVC complex Entity Framework model? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
mvc clientside validation for nested (collection) properties
I have a view displaying fields form a complex Entity Framework model with a few EntityCollection objects in it. They've proven to be very problematic because the Set method doesn't work during model binding, which is described (and my solution provided in the answers) in this stackoverflow question.
Now I'm having a problem with required field validation using the [Required] ValidationAttribute metadata on the TEntity type in the EntityCollection. I think it originated because when you use strongly typed Html Helpers for individual items in a collection like Html.TextBoxFor(model => model.Application.References.ElementAt(i).FullName) (where i is the int used in a for loop), the rendered output has an input with name="FullName" rather than name="Application.References[0].FullName". That wasn't applying the metadata on the References class's properties at all. So my solution was to use the regular html helpers like so: #Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName).
Now, only the server side validation created by the [Required] attributes works. The only way I can get client side validation is to manually add the data-val and data-val-required attributes to the rendered input as follows: #Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName, new { data_val="true", data_val_required="The Full Name for Reference " + i + " is required."}). That seems like too much code-smell to me. Surely there's a better way to do this?
Here are excerpts from my code for details:
EF Model
public class Application
{
//...EF framework code...
public EntityCollection<Reference> References
{
//get, set
}
}
public partial class Reference : EntityObject
{
public global::System.String FullName
{
//get, set
}
}
ReferenceMetaData.cs
[MetadataType(typeof(ReferenceMetadata))]
public partial class Reference
{
}
public class ReferenceMetadata
{
[Required]
[DisplayFormat(NullDisplayText = "N/A")]
[DisplayName("Full Name")]
public string FullName { get; set; }
//...more properites
}
View
#for (int i = 0; i < 3; i++)
{
#Html.ValidationMessage("Application.References[" + i + "].FullName")
<div class="input-container">
<div class="editor-label">
#Html.LabelFor(model => model.Application.References.ElementAt(i).FullName)
</div>
<div class="editor-field">
#Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName, new { maxlength = "100" })
</div>
</div>
#* more view code...*#
}
This code is without manually setting the data-val and data-val-required attributes on the #Html.TextBox. Adding that as follows enables client side validation, but at the cost of not sharing the same validation message as the default (or any I might specify in the [Required(ErrorMessage = "Custom message")] metadata):
#for (int i = 0; i < 3; i++)
{
#Html.ValidationMessage("Application.References[" + i + "].FullName")
<div class="input-container">
<div class="editor-label">
#Html.LabelFor(model => model.Application.References.ElementAt(i).FullName)
</div>
<div class="editor-field">
#Html.TextBox("Application.References[" + i + "].FullName", Model.Application.References.ElementAt(i).FullName, new { maxlength = "100", data_val="true", data_val_required="The Full Name for Reference " + i + " is required." })
</div>
</div>
#* more view code...*#
}
I believe I accidentally made a duplicate of mvc clientside validation for nested (collection) properties, the answer provided to that question worked perfectly for me.

Resources