RadioButton comes checked automatically - c#

I used RadioButtonFor like this
#Html.RadioButtonFor(m => m.Content, Model.Content, new { #name = "rb", #class = "answer-radio", #id = "answer-" + Model.Counter,#checked = "false" })
#Html.Label(Model.Content, new { #for = "answer-" + Model.Counter })
#Html.HiddenFor(m => m.Content)
But this radiobutton comes "checked". How can i disable it ?

Are you sure you want a radio button? Seems like you might be after a Checkbox...
For a boolean, I would set up a 2 radiobuttons like so:
#Html.RadioButtonFor(m => m.Content, true, new { #id = "rb1" })
#Html.RadioButtonFor(m => m.Content, false, new { #id = "rb2" })
The html helper will figure out which one to check. If Model.Content is of another type, use different values for the radiobuttons and MVC will still figure out which one to check.
Edit:
Actually, it's even simpler than that. If the you use the Html helper RadioButtonFor method, it will check the radiobutton if it finds a value that matches the Model property. So you've specified your RadioButton as always having the same value as your Model property so it will always be checked (and that is the value that will be posted back in the form)
So, if you wanted a bool to default to false, and only return true if checked you could do just this:
#Html.RadioButtonFor(m => m.Content, true, new { #id = "rb1" })
where Content is originally set to false.
You can do it with other value types as well. e.g. if Content was a string, initially set to null, this would post back "RadioButtonChecked" only if the radio button was checked. Null otherwise:
#Html.RadioButtonFor(m => m.Content, "RadioButtonChecked", new { #id = "rb1" })

Adding "checked" to the attribute list will make it checked no matter what. I would make sure the value in your model is either null or false.

Related

How to take value from DataValueField() of a Kendo().DropDownList() on change event

I'm using kendo grid and editor template for showing my data. in editor I've given id to DataValueField() and name to DataTextField() of kendo dropdown list. In change event, I'm not able to get the DataValueField(). see the following code
This is my editor template MemoCarrier.chtml
#using System.Collections
#(Html.Kendo().DropDownList()
.DataValueField("PARTNERID")
.DataTextField("PARTNERNAME")
.Name("AIRLINENAME")
.BindTo((IEnumerable)ViewBag.lstAirline)
.HtmlAttributes(new { maxlength = "", #class = "MNum" })
.OptionLabel("-Select-Flight ")
.Filter(FilterType.Contains)
.Events(e =>
{
e.Change("MemoCarrier");
})
)
Here is my on change function
function MemoCarrier(e) {
var AirlineName = this.value();
alert(AirlineName) //it displays PARTNERNAME instead of PARTNERID
}
Currently I'm getting name ie;DataTextField() value. instead of that, I need DataValueField().
Thanks for suggestions in advance!
so based on your comment the easiest way to do this would probably use the data-bind attribute to simplify the process of binding the model. Assuming you are using the MVC helper for the grid as well.
so taking your code and adding this:
#(Html.Kendo().DropDownList()
.DataValueField("PARTNERID")
.DataTextField("PARTNERNAME")
.Name("AIRLINENAME")
.BindTo((IEnumerable)ViewBag.lstAirline)
.HtmlAttributes(new { maxlength = "", #class = "MNum", data_bind="value:{yourProperyNameHere}" })
.OptionLabel("-Select-Flight ")
.Filter(FilterType.Contains)
)
So hopefully you can see all I am doing is adding a new HtmlAttribute property to the control for you. All you need to do is put whatever property is meant to be the value for this.
Depending on if this value is a complex (object) or simple (string, int etc) primitive type you may need to set the Primitive property to true so that only the valuefield e.g the id you are assigning is bound back to the grid's row model.

Id with RadioButtonFor

I'm trying to deal with radiobutton ids in asp MVC 5
I'm working like this
Example model
class A
{
public bool? radio { get; set; }
}
and in razor view
#Html.RadioButtonFor(x => x.NeutroBT, Model.NeutroBT, new { #id = "True"})
#Html.RadioButtonFor(x => x.NeutroBT, !Model.NeutroBT, new { #id = "False})
It doesn't cause problem, but I'm working in an editortemplate, and I want it to have generated id, to access in some way like #Html.IdFor(x => x.NeutroBT, true) and #Html.IdFor(x => x.NeutroBT, false) from the other views, just preventing changes in the future
Is some like this possible? I pass a lot of time searching and I didn't get anything similar
If is not possible, what is the best way to deal with it?
thanks!
There is no need to use an id attribute. Instead you can just use the name attribute to select or set the value via javascript (and in any case, #Html.IdFor() will only ever returnNeutroBT, not theidthat you override in theRadioButtonFor()` method so it cannot be used in your case)
In addition, the 2nd parameter of RadioButtonFor() should be true or false (not Model.NeutroBT and !Model.NeutroBT).
And to associate a label with the button, you can wrap it in a <label>, for example
<label>
#Html.RadioButtonFor(x => x.NeutroBT, true, new { id = ""})
<span>Yes</span>
</label>
<label>
#Html.RadioButtonFor(x => x.NeutroBT, false, new { id = ""})
<span>No</span>
</label>
Note that new { id = "" } removes the id attribute and prevents invalid html due to duplicate id attributes.
Then to access the selected value using jQuery
var selectedValue = $('input[name="' + #Html.NameFor(x => x.NeutroBT) + '"]:checked').val();

How to make a boolean HTML5 attribute 'conditional' in ASP.Net MVC?

I'm using ASP.Net MVC (5.2.3.0) to create a form field:
#Html.TextBoxFor(
x => x.UserName,
new {
#class = "form-control",
placeholder = "Enter your username",
required = true,
autofocus = true
});
So far so good, but now I like to make the autofocus attribute conditional. How would I go about?
The autofocus attribute is a boolean attribute and W3C states:
Note: The values "true" and "false" are not allowed on boolean
attributes. To represent a false value, the attribute has to be
omitted altogether.
The following doesn't work in MVC, because it still renders the autofocus attribute causing the browser to do the auto focus:
autofocus = String.IsNullOrEmpty(this.Model.UserName)
autofocus = String.IsNullOrEmpty(this.Model.UserName) ? null : ""`
Does anybody know how to solve this problem?
You could create an attribute dictionary somewhere before in the view:
#{
var usernameAttrs = new Dictionary<string, object>
{
{"class ", "form-control"},
{"placeholder ", "Enter your username"},
{"required", true},
};
if (String.IsNullOrEmpty(this.Model.UserName))
{
usernameAttrs.Add("autofocus", true);
}
}
and use it in the TextBoxFor()
#Html.TextBoxFor(x => x.UserName, usernameAttrs);
check your condition in controller and if true set ViewBag.Tag = autofocus and then in your view
#Html.TextBoxFor(
x => x.UserName,
new {
#class = "form-control",
placeholder = "Enter your username",
required = true,
ViewBag.Tag
});

Input value does not show up in MVC

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

Conditionally disabling a Bootstrap control

I am trying to enable or disable a control based on a value from a model. I was hoping I could use disabled="disabled" when I want the control to be disabled, or else, disabled = "" when I want it to work.
#Html.DropDownListFor(x => x.AccountTypeId, Model.AccountTypes, "Select One", new { #class = "form-control", #disabled = Model.ClosedDate.HasValue ? "disabled" : "" })
But this doesn't work, and the control is always disabled, regardless of the value. Is there a way I can do this, without using JavaScript or something?
Unfortunately the way the disabled attribute works in HTML is if present on an element, regardless of value, it marks the element as disabled.
So, you could modify your code to something like this:
#Html.DropDownListFor(x => x.AccountTypeId, Model.AccountTypes, "Select One", (Model.ClosedDate.HasValue ? new { #class = "form-control", #disabled = "disabled" } : new { #class = "form-control" })
Not the prettiest but should get the job done
One more point to remember is the fact that setting the input as disabled prevent the form to submit the value of the input, and you might get into some trouble when the mvc bind your model, because they will insert default values for the property disabled. If you want to post the value back to your controller, use readonly attribute instead of disabled.

Categories

Resources