Rendering a <textarea /> using data annotations - asp.net-mvc

I'd like to render a text area for one of the fields in my model.
I've tried applying [DataType(DataType.MultilineText)] to my field, but this doesn't do the trick.
At the moment, I am rendering the text area manually but I'd much prefer to use Html.EditorFor. Any suggestions?

[DataType(DataType.MultilineText)] only works if you use Html.EditorFor helper in your view:
Sample:
Model
[DataType(DataType.MultilineText)]
public string Description { get; set; }
View
Html.EditorFor(m => m.Description)

Why not use:
#Html.TextAreaFor(model => model.YourProperty)
EditorFor helper is sort of "smart" helper and it's basing the rendering based on the underlying type of the property. If you want to enforce it to render a specific html input type then use other helpers.

Related

MVC EditorFor only rendering single property, not EditorTemplate

Got some issues with EditorFor and I can't understand the behaviour.
I have a list of objects where I want to render an editor template based on that list.
According to https://stackoverflow.com/a/26069912 the EditorFor should render the template for each object.
public class Person
{
public string Name { get; set; }
public string PhoneNumber { get; set; }
}
The template
#model EditForTest.Models.Person
Name
<td>#Html.TextBoxFor(x => x.Name)</td>
Phone
<td>#Html.TextBoxFor(x => x.PhoneNumber)</td>
In the view I got a view model containing a list of persons. When I use
#Html.EditorFor(x => x.Persons)
the template is not rendered and somehow the first property is rendered on the page.
As #StephenMuecke mentioned I had a problem with my editor template.
Once the template was moved to the correct path /Views/Shared/EditorTemplates/Person.cshtml the editor was rendered properly.

How to FluentBootstrap validation in MVC

Is it possible to use validation messages in FluentBootstrap on form element level?
When I use:
#using (var form = Html.Bootstrap().Form().Begin())
{
#form.InputFor(m => m.Name)
#form.Submit()
}
There is no validation, except validation error class on input tag. I want to show validation message for Name property and has-error class on form-group container.
Just add #form.ValidationSummary() where you want the general validation summary to appear. There is an example of this as well as some other examples here: https://github.com/daveaglick/FluentBootstrap/blob/develop/FluentBootstrap.Tests.Web/Views/MvcTests/MvcForms.cshtml
If there is no particular reason to use InpuFor you can do the following.
#form.EditorFor(m => m.Name, addValidationMessage: true)
This will show what error message you put on your Name property's attribute.
Such as
[StringLength(50, ErrorMessage="Cannot be longer than 50 character.")]
public string Name { get; set; }
I personally use the base validation helper
#form.InputFor(m => m.Password, FormInputType.Password)
#Html.ValidationMessageFor(m => m.Password)
I can set the type of the input unlike the case when I use EditorFor

How to hide primary key input field using editorfor

Hi I'm using Editorfor() to make a little form that submits view model to the controller. Editorfor() nicely prints input fields of the model but it also prints primary key field. So I want to hide primary key field.
#Html.EditorFor(m=>m.viewmodel)
this is markup that I have.
#Html.HiddenFor(m => m.viewmodel.Id);
#Html.EditorFor(m=>m.viewmodel)
have tried this but does not work. and I wanted to make an approach directly to the model but I'm using EF Designer, so I'm not sure where to begin. Please give me an advice.
Try this:
[HiddenInput(DisplayValue = false)]
public int ProductID { get; set;
Use a custom editor template. For example:
MyViewModel.cshtml (stored in ~/Views/Shared/EditorTemplates folder, structured like a partial view):
#model MyViewModel
#Html.HiddenFor(m => m.Id)
#Html.EditorFor(m => m.Property1)
#Html.TextboxFor(m => m.Property2)
#Html.TextAreaFor(m => m.Property3)
// Whatever else you want in the template
Then you can just call EditorFor on your model in your view that needs to use it and MVC will know to use your custom template:
#Html.EditorFor(m => m.MyViewModel)
To use a custom display template that isn't based on the name of the type, you can also use the [UIHint] attribute as described here: http://www.growingwiththeweb.com/2012/12/aspnet-mvc-display-and-editor-templates.html
Don't create any field for your key attribute. Without a field to check, the validation has nothing to complain about. Later you can supply a value for the primary key in the controller.

DataAnnotation validation attributes do not work when field is rendered in partial view

I have a simple model that has a string property that has validation put on it via dataannotations.
When this is rendered in the main form validation works as expected. If I move the field to a partial view the validation no longer works.
My main and partial views contain this:
#Html.EditorFor(m => m.MyNumber)
#Html.ValidationMessageFor(m => m.MyNumber)
My model looks like this:
[Remote("IsValidMyNumber", "Home",
ErrorMessage = "This does not appear to be a valid Number.")]
public string MyNumber { get; set; }
Simply moving the code in the view from the view to the partial view will cause the validation to not be called.
Why is this?
Because I am rendering the partial in a #section of the page it is not being included in my Form tags. So therefore is not being validated.

How do I set up a check box with ASP MVC in my view?

I have the following class:
public class City {
public string Name { get; set; }
public bool CityValid { get; set; }
}
I know how to set up the name but how can I set up the CityValid field so it acts like a checkbox. I'd like to do this without using HTML helpers.
If you really don't want to use helpers, you would use a normal HTML input tag:
<input type="checkbox" id="CityValid" name="CityValid" value="#Model.CityValid" />
<input type="hidden" id="CityValue_Hidden" name="CityValid" value="false" />
The name attribute has to match your property name so that the model binder will pick it up correctly when you post back to the server.
When you use the helpers, something similar to the above markup will be generated. The hidden field is there so that a value is always sent with the form post data, regardless of whether you check the box or not (if you leave the box unchecked, no value gets sent by default, not even a 'false').
However, unless you're doing something really weird, I'd recommend you stick to using the helpers. Either:
#Html.CheckboxFor(m => m.CityValid)
or
#Html.EditorFor(m => m.CityValid)
In your view you could use the EditorFor helper:
#model City
...
#Html.EditorFor(x => x.CityValid)
The default editor template for a boolean field generates a checkbox.
You could first create it with a HTML-helper, look at the markup that gets created when running the page, and then recreate that markup...

Resources