model
[DefaultValue(true)]
public bool IsActive { get; set; }
view
#Html.CheckBoxFor(model => model.IsActive)
html output and that is my expected
<input data-val="true" data-val-required="Yayında alanı boş bırakılmamalıdır!" id="IsActive" name="IsActive" type="checkbox" value="true" class="valid">
but checkbox is not checked. I cant understand why? What am I missing?
Thanks.
Per the MSDN
A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
Related
I have a function, that collect the value of the controls of some view(.cshtml)
private string ExtractEmailId(FormCollection form)
{
var value = form["CkbQuestion1"];
return value;
}
I am receiving value of Checkbox as "true,false". I need the value of that control.
How I can have that?
Any Idea please.
I expect you required the below code. Please replace your code with this.
[HttpPost]
public string ExtractEmailId(FormCollection form)
{
var value = form["CkbQuestion1"];
return value;
}
And you view will be similar to
#Using(Html.Beginform("ExtractEmailId"))
{
<input type="checkbox" name="CkbQuestion1" />
<input type="submit" value="Submit" />
}
CkbQuestion1 must be set as value of name attribute for checkbox. It will look like this in your Html.
<input type="checkbox" name=""/>
it will return Empty string if you didn't define the value of value attribute. If you don't define value attribute then it will post the NULL to server.
if you have property for that field than simply change your mark up and write
#Html.EditorFor(td => td.PropName)
and now you can access your checkbox value in controller.
I am trying to use the RemoteAttribute to validate a data element serverside using JSON.
My data field is:
[Display(Name = "My Number")]
[Required]
[Remote("IsValidMyNumber","Home",ErrorMessage="Bummer")]
public string MyNumber { get; set; }
My controller is:
public JsonResult IsValidMyNumber(string MyNumber)
{
var test = services.ValidateMyNumber(MyNumber);
return Json(test,JsonRequestBehavior.AllowGet);
}
My view is:
<div class="editor-field">
#Html.EditorFor(model => model.CheckInformation.MyNumber)
#Html.ValidationMessageFor(model => model.CheckInformation.MyNumber)
</div>
The HTML generated is:
<input class="text-box single-line" data-val="true" data-val-remote="Bummer"
data-val-remote-additionalfields="*.MyNumber" data-val-remote-url="/Home/IsValidMyNumber"
data-val-required="The Number field is required." id="CheckInformation_MyNumber"
name="CheckInformation.MyNumber" type="text" value="" />
When I debug and step inside my controller the "MyNumber" parameter is null even though I have text in the textbox that this represents.
I know that the name has to be the same in the textbox as in the parameter and I have validated that.
Any ideas?
Seems like the generated name of your input field is:
name="CheckInformation.MyNumber"
That's probably because your view model is a parent model of what you have shown in your question and you used something along the lines of:
#Html.TextBoxFor(x => x.CheckInformation.MyNumber)
So make sure you have specified this prefix or the default model binder will never be able to rehydrate the values:
public ActionResult IsValidMyNumber([Bind(Prefix = "CheckInformation")] string myNumber)
{
var test = services.ValidateMyNumber(myNumber);
return Json(test, JsonRequestBehavior.AllowGet);
}
You can also receive the value of myNumber to pass your view model in the action parameter, Like.
It works for me.
public ActionResult IsValidMyNumber(YourViewModel vm)
{
var test = services.ValidateMyNumber(vm.myNumber);
return Json(test, JsonRequestBehavior.AllowGet);
}
have a simple ViewModel with three properties like so:
public bool RememberMe { get; set; }
In my view I have a simple #Html.CheckBoxFor(p => p.RememberMe)
I am using Client Side validation enabled using Html.EnableClientValidation();
Why is this being set as a required field?
Try a nullable bool.
public bool? RememberMe { get; set; }
With reference types there are a number of default validation rules applied. If a reference type is not nullable, it becomes required by default. The best illustration of this is if you use a textbox to display some properties (not something you would do in your site, but good for testing purposes):
Model:
public bool? MyBool { get; set; }
public int MyInt { get; set; }
View:
#Html.TextBoxFor(p => p.MyBool)
#Html.TextBoxFor(p => p.MyInt)
You can see from a view source what happens on the page:
<input id="MyNullBool" name="MyNullBool" type="text" value="">
<input data-val="true" data-val-required="The MyBool field is required." id="MyBool" name="MyBool" type="text" value="False">
<input data-val="true" data-val-number="The field MyInt must be a number." data-val-required="The MyInt field is required." id="MyInt" name="MyInt" type="text" value="0">
The nullable bool has no validation attributes, whereas the bool has a data-val-required tag. The int has a data-val-required tag and a data-val-number attribute
Of course, on a checkbox this is all pretty redundant as it can only be checked (true) or not checked (false) so a required tag isn't much use.
#Html.CheckBoxFor(c => c.TermsAndConditions, new { required = "required" })
#Html.ValidationMessageFor(c => c.TermsAndConditions, "you must agree to terms and conditions of Service.)"
I have created a strongly typed MVC3 Razor view using the scaffolding code.
The model is a POCO with a base type of PersistentEntity which defines a property called Created, Updated and Id.
Id is an int, Created and Updated are DateTime.
I am using Html.HiddenFor to create the hidden field on the view.
#Html.HiddenFor(model => model.Id)
#Html.HiddenFor(model => model.Created)
#Html.HiddenFor(model => model.Updated)
On the page, the hidden input is being rendered properly, with the Id being set in the value.
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="12">
However when the page is submitted to the controller [HttpPost]Edit(Model model) the Id property is always 0. Created and Updated are correctly populated with the values from the View.
This should be 12 in the case of the example in this post. What is going wrong?
I am aware that I can change the method signature to [HttpPost]Edit(int personID, Person model) as the personID is in the get string, however why does the model not get populated with the hidden field?
Update
The problem was that the setter on PersistentEntity was protected, ASP could not set the property, and swallowed it. Changing this to public has solved the problem.
public abstract class PersistentEntity
{
public virtual int Id { get; protected set; }
public virtual DateTime Created { get; set; }
public virtual DateTime Updated { get; set; }
}
public virtual int Id { get; protected set; }
protected set; <!-- That's your problem. You need a public setter if you want the default model binder to be able to assign the value.
This is the model with it's validation:
[MetadataType(typeof(TagValidation))]
public partial class Tag
{
}
public class TagValidation
{
[Editable(false)]
[HiddenInput(DisplayValue = false)]
public int TagId { get; set; }
[Required]
[StringLength(20)]
[DataType(DataType.Text)]
public string Name { get; set; }
//...
}
Here is the view:
<h2>Create</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Tag</legend>
<div>#Html.EditorForModel()</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
And here is what get's renderd:
<form action="/Tag/Create" method="post">
<fieldset>
<legend>Tag</legend>
<div><input data-val="true" data-val-number="The field TagId must be a number." data-val-required="The TagId field is required." id="TagId" name="TagId" type="hidden" value="" />
<div class="editor-label"><label for="Name">Name</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-length="The field Name must be a string with a maximum length of 20." data-val-length-max="20" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span></div>
...
</fieldset>
</form>
The problem is that TagId validation gets generated althoug thare is no Required attribute set on TagId property. Because of that I can't even pass the client-side validation in order to create new Tag in db.
What am I missing?
I found the answer. Just add this to Application_Start:
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
Make the view-model value-types nullable. Then they won't be Required by default.
Note also if you put the attribute 'required="false"' in html 5 (if you set html 5 in your doctype meta data), it will see "required" and make it required. You can use dojo-data-props="required:false".
frennky's solution only removed data-val-required but in my case I still had data-val-number and data-val
I had to add the two lines below to Application_Start to get rid of everything.
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());
The problem is that the value of the hidden field is empty. This shouldn't happen if you use integer type. I suppose that the TagId property is defined as a nullable type in the Tag class. So either assign it a value before rendering the view or use an integer type:
[MetadataType(typeof(TagValidation))]
public partial class Tag
{
public int TagId { get; set; }
public string Name { get; set; }
}
so that the generated hidden field looks like this:
<input
data-val="true"
data-val-number="The field TagId must be a number."
data-val-required="The TagId field is required."
id="TagId"
name="TagId"
type="hidden"
value="0"
/>
Also normally client side validation shouldn't be triggered for this hidden field.
jquery validate target cheking "disabled" html attribute.
$(function () {
$("#TagId").attr("disabled", "disabled")
});
or use Nullable.
hope this code!
With MVC4 you can also use this:
#{ Html.EnableClientValidation(false); }
#Html.EditorForModel()
#{ Html.EnableClientValidation(true); }
Make your Model or View-Model property value-types "nullabel". This will solve your problem.One important thing that remove "required" attribute from your tag otherwise it will take i "required"
Example:-
public class ViewModle
{
public int? foo{get;set;}
}
Here in example foo is integer nullable type, this will no longer required in mvc.
Hope this will help you.
I had the unfortunate experience that my model attributes were suddenly required causing my web APIs to return 400 errors when attributes were missing from web requests.
I found out this was caused by an accidental change of the "Nullable" setting in the project (Project properties -> Build -> Nullable: Enable"
After changing Nullable to Disable, all was good again.
I found a more detailed explanation of the issue here:
https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts