I just started using MVC3. I am using fields like this:
<div class="editor-field">
#Html.TextBoxFor(model => model.Status.RowKey, new { disabled = "disabled", #readonly = "readonly" })
</div>
Which creates this HTML
<div class="editor-field">
<input disabled="disabled" id="Status_RowKey" name="Status.RowKey" readonly="readonly" type="text" value="0001" />
</div>
But I notice all the input fields have the same width which is about 160px. Is there a way I can make them wider and why do they default to this. In my editor-field class I
You could set the size property:
#Html.TextBoxFor(model => model.Status.RowKey, new { size = 200, disabled = "disabled", #readonly = "readonly" })
Probably more appropriate would be set the input width in css and give the textbox a class, like:
#Html.TextBoxFor(model => model.Status.RowKey, new { #class = "myClass", disabled = "disabled", #readonly = "readonly" })
<style>
input.myClass { width = 200px; }
</style>
If your fields do not have any styling your browser will apply it's own default width to them. You need to add a style to them
By adding an external CSS file containing
div.editor-field input { width: 300px; }
Or by inline (not recomended)
#Html.TextBoxFor(model => model.Status.RowKey, new { disabled = "disabled", #readonly = "readonly", style="width:300px" })
You can use CSS for that.
For example:
input#StatusRowKey /* matches only the textfield with id StatusRowKey */
{
width: 250px;
}
input /* matches all input types */
{
width: 200px;
}
input.Normal /* matches all input fields with class="Normal" */
{
width: 100px;
}
Related
I am having an issue with the asp-validation-for on a span element in the code below.
<div class="form-group">
#if (Model.Property.Options.ElementAt(i).OptionTypeId == PropertyOptionType.CheckBox)
{
var ItemChecked = (Model.Property.Options.ElementAt(i).OptionValue == "on") ? " checked" : "";
<text><input type="checkbox" class="form-check-input" name="Options[#i].Code" id="Options[#i].Code" #ItemChecked data-val="false" />
<label class="form-check-label" for="Options[#i].Value"> #(Model.Property.Options.ElementAt(i).OptionValue)</label></text>
}
else if (Model.Property.Options.ElementAt(i).OptionTypeId == PropertyOptionType.List)
{
<label class="control-label">
#Model.Property.Options.ElementAt(i).OptionValue
</label>
<select class="form-control" name="Options[#i].Code"></select>
}
<span class="text-danger field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Options.#(i).Code"></span>
When it is rendered in HTML it comes out as
<span class="text-danger field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Options.[3].Code"></span>
But no validation error messages ever land in the span. They are picked up in a page level validation summary when set to all so the unobtrusive code is all working correctly but the ID of the span is being broken by the square brackets.
Any ideas?
Thanks
Mark
To display Validation Summary, you need to include:
#Html.ValidationSummary(false/*excludePropertyErrors?*/, "", new { #class = "text-danger" })
To display field specific error message, you need to use ValidationMessageFor:
#Html.EditorFor(model => model.StudentName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.StudentName, "", new { #class = "text-danger" })
See this tutorial
Don't manually generate the validation span, html helpers will do that for you... you would need something like this:
#for (int i = 0; i < Model.Property.Options.Count(); i++)
{
/* this one generates the input */
#Html.EditorFor(m => m.Property.Options[i], new { htmlAttributes = new { #class = "form-control" } })
/* this one generates the validation message */
#Html.ValidationMessageFor(m => m.Property.Options[i], "", new { #class = "text-danger" })
}
I'm using the existing template of the ASP.NET MVC project, but I have a textarea field that I want it to be resizable in both directions with minimum height and width.
I'm using EditorFor in the markup and specified [DataType(DataType.MultilineText)] in the model class.
<div class="form-group">
#Html.LabelFor(model => model.Body, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Body, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Body, "", new { #class = "text-danger" })
</div>
</div>
I'm new to bootstrap, but using the page inspector I found that the margin taking the whole width and I can't change its value, I changed the height value and it works but with fixed width:
textarea.form-control {
height: 400px;
margin: 0px;
resize: both;
}
It appears you're using razor syntax, so you could try this for the TextArea:
#Html.TextAreaFor(model => model.Body, new { #class = "form-control" })
I'm having an issue where my kendo datetimerpicker is rendering differently between when I run on my local machine and when the webpage has been published
Script:
<script>
$(document).ready(function () {
$("#datetimepicker").kendoDateTimePicker({
animation: {
close: {
effects: "faceOut zoom:out",
duration: 300
},
open: {
effects: "faceIn zoom:in",
duration: 300
}
}
});
});
HTML:
<div class="form-group">
#Html.LabelFor(model => model.Highwatermark, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Highwatermark, new { htmlAttributes = new { #class = "form-control", #id = "datetimepicker" } })
#Html.ValidationMessageFor(model => model.Highwatermark, "", new { #class = "text-danger" })
</div>
</div>
Running on local machine:
Published:
It still works if I click where the calendar and clock should be but I'm not sure why the images aren't showing up.
I'm also not sure why the datetimepicker is stretching so far over in both cases.
The form-control CSS class is related to the expanding:
http://docs.telerik.com/kendo-ui/third-party/using-kendo-with-twitter-bootstrap#use-form-control-bootstrap-css-class
You can restore the standard DateTimePicker width of 15em with a custom CSS rule:
.k-datetimepicker.form-control {
width: 15em;
}
If you need only some DateTimePickers to maintain their default width, apply another custom CSS class in htmlAttributes in addition to form-control and use it in the CSS selector:
.k-datetimepicker.form-control.my-no-expand {
width: 15em;
}
Assuming that the main theme images (sprite.png and sprite_2x.png) do exist on the production server, then the problem with the missing icons can be caused by the CSS bundling configuration:
http://docs.telerik.com/kendo-ui/aspnet-mvc/fundamentals#css-bundling
Check your kendo CSS files. Sometimes the relative addresses need modification to apply corrently. It is common issue. For example in css file you have something like:
background-image:url('Bootstrap/sprite.png')
which should change to
background-image:url('2015.3.930/Bootstrap/sprite.png')
I have am using html helper fields below, my issue I need the make these hiddenfor elements not hidden when checkbox is checked.
#Html.HorizontalFormFieldFor(model => model.InsaatHizmetBedeli)
<div class="control-group">
#Html.LabelFor(model => model.tadilatMi, new { #class = "control-label" })
<div class="controls">
#if (!Model.tadilatMi.HasValue)
{
Model.tadilatMi = false;
}
#Html.CheckBoxFor(model => model.tadilatMi.Value, new { #Name="tadilatmi" });
</div>
</div>
#Html.HiddenFor(model => model.myHiddenProperty)
here is my jquery code:
$("input[name='tadilatmi']").on("change", function () {
if ($("input[name='tadilatmi']").is(":checked")) {
$("#myHiddenProperty").show()
}
})
of course it not works.. how can I achieve this ?
Your generating an input with type="hidden" which is always hidden. The jQuery.show() method is for toggling the display of elements styled with display:none; and its changes it to display:block;
You could do this by changing the type attribute
if ($("input[name='tadilatmi']").is(":checked")) {
$("#myHiddenProperty").attr('type', 'text')
}
or by making the input type="text" and styling it as hidden
#Html.TextBoxFor(model => model.myHiddenProperty)
with the following css
#myHiddenProperty {
display: none;
}
and then your original script will work.
I suspect however that you want to toggle the visibility back if the checkbox is then unchecked, in which case you should have an else block
if ($("input[name='tadilatmi']").is(":checked")) {
$("#myHiddenProperty").show()
} else {
$("#myHiddenProperty").hide()
}
Side note: your using an awful hack in order to make you checkbox bind to a nullable bool property (by chaninging the name attribute) and your label does not even work as a label (clicking on it will not toggle the checkbox). I recommend you use a view model with
public bool Tadilatmi { get; set; }
and in the view simply use
#Html.LabelFor(m => m.Tadilatmi , new { #class = "control-label" })
<div class="controls">
#Html.CheckBoxFor(m => m.Tadilatmi);
</div>
and change the script to (which is more efficient)
var hiddenElement = $('#myHiddenProperty');
$('#tadilatmi').change(function () {
if ($(this).is(":checked")) {
hiddenElement.show()
} else {
hiddenElement.hide()
}
})
Your myHiddenProperty property could then include a foolproof [RequiredIfTrue("Tadilatmi")] or similar conditional validation attribute.
I need to create the textarea field at my form
<textarea class="img-shadow" cols="20" id="Message" name="Message" rows="2">Message</textarea>
I write code
#Html.TextAreaFor(model => model.Message, new { #class = "img-shadow" })
but I get empty textarea without any text
<textarea class="img-shadow" cols="20" id="Message" name="Message" rows="2"></textarea>
How can I add text in?
The content of the text box will be whatever the value of model.Message is.
This should be set in your Action method in the Controller, and passed to the View
public ViewResult ActionName() {
var model = new ViewModel();
model.Message = "Text Area Content";
return View(model);
}
As a test just output model.Message in the View, it will be empty.
<p>#Model.Message</p>
Then #Html.TextAreaFor(model => model.Message, new { #class = "img-shadow" }) will output
<textarea class="img-shadow" cols="20" id="Message" name="Message" rows="2">Text Area Content</textarea>
After much scratching around I managed to get it done this way.
#{ var _address="Line 1\nLine 2\n Line 3"; }
#Html.TextArea(Model.xxxx , new { id="address" } )
<script>
var x = #Html.Raw(Json.Encode( _address )) ;
document.getElementById("address").value = x;
</script>
If you haven't any control characters like newline \n you can replace var x=#Html...... with var x = "Text" ;
#Html.TextAreaFor(model => model.Message, new { #class = "img-shadow", value = "added text" })
But If you initialize your model in controller Get method, then it will add text to your textarea, automaticly.
Check these tutorials
you add the default value into it.. by
#Html.TextAreaFor(model => model.Message, new { #class = "img-shadow",#value = "added text" })
In Controller Write
ModelVariable.Message= Server.HtmlDecode(ModelVariable.Message);
#Html.TextAreaFor(model => model.Message, new { #class = "img-shadow" })
It has worked for me