Here is my code:
_testPartial.cshtml(Partial view)
#Html.RadioButtonFor(model => model.CardType, "G", new { #checked = #Model.IsGold})
#Html.RadioButtonFor(model => model.CardType, "P", new { #checked = #Model.IsPlatinum})
#Html.RadioButtonFor(model => model.CardType, "B", new { #checked = #Model.IsBusiness})
Controller
public PartialViewResult ShowCardtype()
{
Model cardType= new Model {
IsGold=true,
IsPlatinum=false,
IsBusiness=false};
return PartialView("_testPartial",cardType);
}
The expected behavior is, the GOLD radio button option should have been selected in the form. But it always selects the last option. I tried returning "checked" string, but no luck. Appreciate any help.
the second option is the selected option, you don't need the new part.
#Html.RadioButtonFor(model => model.CardType, "G")
#Html.RadioButtonFor(model => model.CardType, "P")
#Html.RadioButtonFor(model => model.CardType, "B")
when you load the view set model.CardType to G, P, or B and it will select the correct radio button
Related
Say if I have this textbox
#Html.TextBoxFor(x => x.Name, new {id = "add-name", data_parsley_maxlength = "1" })
How do I write my LabelFor so that it references "add-name"?
Or do I have to use #Html.Label?
I have a problem for which I just can't find a solution.
Checking button like this works:
#Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3
? new { id = "B", #checked = "checked" } : null)
But I also need to set id, how do I do that? This code:
#Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3
? new { id = "B", #checked = "checked" } : new { id = "B" })
Gives error:
Type of conditional expression cannot be determined because there is no implicit conversion between 'AnonymousType#1' and 'AnonymousType#2'
Which is beyond my understanding.
I suppose there should be a trivial solution to this?
Cast them to object:
Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3
? (object)new { id = "B", #checked = "checked" } : (object)new { id = "B" });
Check this link.
#{string id = ViewData.TemplateInfo.GetFullHtmlFieldId("radioTrue");}
#Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3 ? new { id } : null)
A good example of this is here.
I want the disabled attribute be added to a textbox based on a condition
#Html.TextBoxFor(m => m.kidsNumber, new { (Model.kidsDropDown != "2") ? "#disabled" : ""})
Use
#Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? new {disabled = "disabled"} : null )
Note also if you need to add multiple attributes, then it needs to be in the format (where the attributes are cast to object
#Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? (object)new { #disabled = "disabled", #class="form-control" } : (object)new { #class="form-control" })
If you have multiple textboxes that use the same sets of attributes, you can assign these to variables in the view
#{
object number = new { #type = "number", #class="form-control" };
object disabledNumber = new { #disabled = "disabled", #class="form-control" };
}
and in the form
#Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? disabledNumber : number)
#Html.TextBoxFor(m => m.anotherProperty, AnotherCondition ? disabledNumber : number)
.....
You have not said which attribute you want to set ::
you code is without attribute and you should use
use readonly instead because disabled fields are not posted on form submit
#Html.TextBoxFor(m => m.kidsNumber, new { (Model.kidsDropDown != "2") ? "#disabled" : ""})
For Disabled attribute Use::
#Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? new {disabled = "disabled"} : null )
For readonly Use something like::
#Html.TextBoxFor(m => m.kidsNumber, new { #readonly= (Model.kidsDropDown != "2" ? "readonly" : "")})
I have the following DropDownListFor that displays a list of states for the user to choose from. How can I default the dropdown to be empty. It currently defaults to the first state in alphabetical order ("Alaska")
#Html.DropDownListFor(m => m.User.StateID,
new SelectList(Model.User.States.Where(filter => filter.Active && (filter.CountryID == 1))
.Select(item => new
{
ID = item.StateProvinceID,
Description = item.Name
}),
"ID",
"Description",
Model.User.StateID),
new { #data_bind = "value: user.StateID, enable:!isSiteUser()", tabindex = "10" })
I figured out that one of the posted questions did have the correct answer for my problem. I was just trying to do use it incorrectly. I was adding "string.Empty" to the wrong location. This is the solution.
#Html.DropDownListFor(m => m.User.StateID,
new SelectList(Model.User.States.Where(filter => filter.Active && (filter.CountryID == 1))
.Select(item => new
{
ID = item.StateProvinceID,
Description = item.Name
}),
"ID",
"Description",
Model.User.StateID),
string.Empty,
new { #data_bind = "value: user.StateID, enable:!isSiteUser()", tabindex = "10" })
#Html.DropDownListFor(m => m.Baled, new SelectList(new [] {"Select Type","Option1", "Option2", "Option3"}))
I want to add a css class in the dropdown list.
I have tried this
#Html.DropDownListFor(m => m.Baled, new SelectList(new [] {"Select Type","Option1", "Option2", "Option3"}, new {#class="myclass"}))
But it doesn't work
If anyone can suggest me any other better way to display dropdown list will be ok too.
You have close the bracket at wrong place. you have defined attribute in second paramter of selectlist. actually it should be close first and define in the last parameter of Html.DropDownListFor
#Html.DropDownListFor(m => m.Baled, new SelectList(new[] { "Select Type", "Option1", "Option2", "Option3" }), new { #class = "myclass" })