Given the following MVC5 code:
#Html.DropDownList(name: "Enhanced_Rijndael_AlgorithmID", optionLabel: null, selectList: EnhancedRijndaelAvailableHashAlgorithmList.Select(item => new SelectListItem
{
Value = item.RecordID.ToString(),
Text = item.HashAlgorithm,
Selected = "select" == item.RecordID.ToString()
}), htmlAttributes: new { #class = "form-control" })
Is it possible to set the "selected value" of this list?
The only thing I can think of is somehow setting the Selected value in the SelectListItem set.
Here is the what Ive come up with after being reminded of the DropDownListFor
#Html.DropDownListFor(expression: r => r.AlgorithmID, selectList: EnhancedRijndaelAvailableHashAlgorithmList.Select(item => new SelectListItem
{
Value = item.RecordID.ToString(),
Text = item.HashAlgorithm,
Selected = "select" == item.RecordID.ToString()
}),
htmlAttributes: new { #class = "form-control" }, optionLabel: "Algorithm")
Related
Please look at my code. I am trying to load the default value for the drop-down. value is not accepting from the design. I'm loading data to the combobox from, the account controller, I have added list of items to the Tempdata and then in the design I'm assigning the list to the combo box. So here when loading the form I need to set the default value for the combo box drop down list. Please help
This is how I assigning values from the account controller.
List<Request_Types> RequestTyleList = db.Request_Types.Where(r => r.Status == true).ToList();
List<SelectListItem> ReqTypeDropDown = RequestTyleList.Select(r => new SelectListItem { Text = r.Request_Type, Value = r.Id.ToString() }).ToList();
TempData["RequestTyleList"] = ReqTypeDropDown;
This how I called that TempData in the view:
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
List<SelectListItem> ReqTypes = (List<SelectListItem>)TempData.Peek("RequestTyleList");
}
And this is where I assigning that values to the Combobox in the view
<div class="col-md-6 col-sm-6">
<div class="form-group row">
#Html.LabelFor(model => model.ReqType, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-sm-8">
#Html.DropDownListFor(model => model.ReqType, ReqTypes, new { #class = "js-dropdown" })
#Html.ValidationMessageFor(model => model.ReqType, "", new { #class = "text-danger" })
</div>
</div>
</div>
So I when opening the view, I need to assign the default value to the ReqTypes
Just add the following parameter to assign default value
#Html.DropDownListFor(model => model.ReqType, new SelectList(ReqTypes,"Value","Text","Your_Default_Value"), "Select",new { #class = "js-dropdown" })
Below View Code. when i click on Create button without Selecting Activity. it is not showing validation message. i am using Required attribute in Model Class
<div class="form-group">
#Html.LabelFor(model => model.TransactionTypeID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.TransactionTypeID, new SelectList(ViewBag.GetT, "TransactionTypeID", "TransactionType"), "--Select--", new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TransactionTypeID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ActivityID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.ActivityID, new SelectList(ViewBag.GetA, "ActivityID", "ActivityName"), "--Select--", new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ActivityID, "", new { #class = "text-danger" })
</div>
</div>
//Controller code
public ActionResult Create()
{
List<TransactionTypeMaster> T = new DATransactionTypeMaster().GetListAll();
ViewBag.GetT = T;
List<ActivityMaster> A = new DAActivityMaster().GetListAll();
ViewBag.GetA = A;
return View();
}
Can Any One Help on this. Need To Know Where i m going Wrong
when i click on Create button without Selecting Activity. it is not showing validation message. i am using Required attribute in Model Class
try giving the default '--Select--' options in your drop downs a value of 0.
In your model, make sure your drop down value is set to '[Required]' and also give it the attribute:
[Range(1, Int32.MaxValue, ErrorMessage = "Must Select A Value")]
basically letting it only accept values > 0
I think you TransactionTypeID is int, make that Nullable that would fix this issue. Currently it doesn't work because by default it might be sending zero as a value by using null, it would send null when user doesn't touch this field.
I want to add custom class to Asp.net Razor MVC control #Html.DropDownListFor
I have following code
#Html.DropDownListFor(model => model.is_admin, new SelectList(
new List<Object>{
new { value = "true" , text = "Admin" },
new { value = "false" , text = "User" },
},
"value",
"text",
"false" ))
How can this same control be implemented in Asp.net MVC 5
Add a third parameter called htmlAttributes like following.
#Html.DropDownListFor(model => model.is_admin,
new SelectList(
new List<Object>{
new { value = "true" , text = "Admin" },
new { value = "false" , text = "User" },
}, "value", "text", "false"),
new { #class = "your_class_name" });
Hi I am trying to convert the text entered in textbox to uppercase, but I am not getting the output,please help.
#Html.TextBoxFor(model => model.PanNumber, new { #class = "form-control", #onkeyup = "InputToUpper(this);" })
This should work (remove #onkeyup = "InputToUpper(this);" from the TextBoxFor method)
$('#PanNumber').keyup(function() {
var text = $(this).val();
$(this).val(text.toUpperCase());
});
if you are using "#editorfor" text box then try
#Html.EditorFor(model => model.PanNumber.Alias, new {
htmlAttributes = new { #class = "upper-case" }
})
I want to add a class to a selectfor
but I get an error
#Html.SelectFor(m => m.foo, optionLabel: null, new { #class = "foo12" })
With a text box it works:
#Html.TextBoxFormattedFor(m => m.foo, new { #class = "foo" })
Error I get:
Named argument specifications must appear after all fixed arguments have been specified.
The error is self-explanatory -- any named arguments (in this case "optionLabel") have to come after unnamed ones. So instead of this:
#Html.SelectFor(m => m.foo, // 1
optionLabel: null, // 2
new { #class = "foo12" } // 3
)
I guess you probably want this:
#Html.SelectFor(m => m.foo, // 1
optionLabel: null, // 2
htmlAttributes: new { #class = "foo12" } // 3
)
Edit
Surely you mean DropDownListFor, and not "SelectListFor"? You need to provide the options as well. Something like this:
#{
var selectList = new SelectListItem[]
{
new SelectListItem { Text = "text", Value = "value" },
};
}
#Html.DropDownListFor(m => m.foo,
selectlist: selectlist,
htmlAttributes: new { #class = "foo" }
)