This question already has answers here:
Enabling & disabling a textbox in razor view (ASP.Net MVC 3)
(4 answers)
Closed 5 years ago.
I'm developing an ASP.NET MVC 5 app with Razor, C# and .NET Framework 4.7.
I want to make it a textbox not editable if Model.IsChinaProduct is true.
I have this piece of code in a View:
#Html.TextBoxFor(m => m.Configurations[index].PkgRatio, new { #class = "productClass", #onkeydown = "IsValidKey(event);" #if (Model.IsChinaProduct) disabled})
I want to add the disabled attribute if Model.IsChinaProduct is true, but that code shows me the following error:
Error CS0746 Invalid anonymous type member declarator. Anonymous type
members must be declared with a member assignment, simple name or
member access.
How can I add the disabled attribute if Model.IsChinaProduct is true?
UPDATE:
Maybe disabled is not the right attribute.
AFAIK you can't, because there is no disabled="false", meaning you should do something like that:
#{
var htmlAttributes = Model.IsChinaProduct ? (object)
new { #class = "productClass", readonly = "readonly" }
: new { #class = "productClass", #onkeydown = "IsValidKey(event);" };
}
#Html.TextBoxFor(m => m.Configurations[index].PkgRatio, htmlAttributes)
For setting it ReadOnly, try this:
#{
object displayMode = (Model.IsChinaProduct) ? new { #class = "productClass", #onkeydown = "IsValidKey(event);" }
: new { #class = "productClass", #onkeydown = "IsValidKey(event);" readonly = "readonly"};
#Html.TextBoxFor(m => m.Configurations[index].PkgRatio, displayMode)
}
Dont use TextBoxFor if IsChinaProduct = true
Try DisplayFor combine with HiddenFor instead
like this
#if (Model.IsChinaProduct)
{
#Html.HiddenFor(m => m.Configurations[index].PkgRatio)
#Html.DisplayFor(m => m.Configurations[index].PkgRatio)
}
else
{
#Html.TextBoxFor(m => m.Configurations[index].PkgRatio)
}
Related
I'm trying to use EditorFor custom templates.
I want to create a Int32 and decimal templates to render the inputs with some validations.
This is what I'm trying
#model int?
#Html.TextBoxFor(model => model, null, new { #type="text", #oninput = "this.value=this.value.replace(/[^0-9]/g,'')" } )
And I call it like
#Html.EditorFor(x => x.ExampleIntField)
It renders an <input type="text", oninput="this.value=this.value.replace(/[^0-9]/g,'')"
To here everything works, but when I try to pass extra htmlAttributes like readonly I don't understand how I must receive it in EditorFor template.
Example
#Html.EditorFor(x => x.ExampleIntField, new { htmlAttributes = new { #readonly = "readonly" } } )
I tried this I got the exact same <input type="text", oninput="this.value=this.value.replace(/[^0-9]/g,'')" rendered without readonly attribute
You are using the overload of EditorFor() that passes the object as additionalViewData. You can read that within the template from the ViewDataDictionary
#model int?
#{ var attributes = ViewData["htmlAttributes"]; } // returns { #readonly = "readonly" }
which you could then merge with your existing attributes and use in the TextBoxFor() method.
#{
var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(attributes);
htmlAttributes.Add("oninput", "this.value=this.value.replace(/[^0-9]/g,'')";
}
#Html.TextBoxFor(model => model, htmlAttributes)
Note that TextBoxFor() generates type="text" so there is no need to add it again. In addition, you do not need the leading # unless its a reserved keyword (for example #class = "...")
There is an entity that has public List<DayOfWeek> DefaultDaysOfWeek.
I want to bind this list to a set of 7 checkboxes respectively in the view.
I have not found a default way to do something similar to:
#Html.EditorFor(model => model.DefaultDaysOfWeek, new { htmlAttributes = new { #class = "form-control" } })
I have also tried the CheckBoxList(For) framework.
I believe the reason none of this has worked is because I cannot access this enumeration in the View.
I am trying to populate textarea using the input value. But it is not working. Any solution to this issue?
#Html.EditorFor(model => model.ModelName, new { htmlAttributes = new { #class = "textbox-css", #Value = "ViewData.Model.ModelName" } })
You can remove quotes from #Value = "ViewData.Model.ModelName", because you are sending a string to Value property, not the content of ModelName property.
But why don't you use the TextArea helper? Like:
#Html.TextAreaFor(model => model.ModelName, new { htmlAttributes = new { #class = "textbox-css" })
It's not common set the value property like you are doing, I don't know if it works, actually. I think this is not a good way to do that. You should use the TextAreaFor(x => x.Property) or EditFor(x => x.Property) directly, instead of set the value property, as I said.
you don't need to manually populate the value property with the ViewData, remove the #Value = "ViewData.Model.ModelName". I think the problem is your ViewData name conflict with the property of the model (both have the name "ModelName").
Try changing the ViewData property name to something else. Works for me when I have an issue with unpopulated DropDownListFor
This question already has answers here:
MVC4 input field placeholder
(10 answers)
Closed 7 years ago.
In my ViewModel for my partial view I have Email defined like this:
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
In the view I have this:
#Html.TextBoxFor(m => m.Email, new {#class = "form-control"})
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
I would like something like 'example#company.com' to appear in the textbox as a placeholder or example. But right now it is empty.
You can set a placeholder attribute (HTML5) to the form element. Browsers which supports this feature will add a placeholder text to your textbox.
#Html.TextBoxFor(s=>s.Email, new { #class="form-control", placeholder="sample#ss.com"})
Use this to add a placeholder attribute to your HTML:
#Html.TextBoxFor(m => m.Email, new {#class = "form-control", placeholder = "example#company.com"})
How to include Angular directives in Html Helper in ASP.NET MVC C#.
I dont know how to include it.
What I have done is following :
#Html.EditorFor(model => model.Questions.Question , new{ng_model="quest"})
{{quest}}
But when I look for source code that does not have ng-model directive?
The second Argument of Html.EditorFor is for the extended ViewData.
It is used by the template. The Default template dont use your additional entries.
But it supports htmlAttributes
#Html.EditorFor(model => model.Questions.Question , new { htmlAttributes = new { ng_model="quest" }})
or render it with out any template
#Html.TextBoxFor(model => model.Questions.Question, new { ng_model = "quest" })
To include Angular directives in Html Helper you should pass a null parameter as a second then pass the angular directive in a object.
#Html.DropDownList("ViewBag-Name", null, new { ng_model = "Model-Name"})
Where ViewBag-Name contains a SelectList object pass to the view from the controller.
We can do it very easily.
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #ng_change = "call()" ,#class="form-control" }})
#Html.TextBox("Text1", null, new { ng_model = "model1" })