I am pretty new to MVC. In my form i have a disabled dropdown control whose value is not getting passed to the Model during submit as it is disabled. I tried using a hidden field like below
#Html.DropDownListFor(model => model.DepartmentID, new List<SelectListItem> { new SelectListItem { Text = "Item 1", Value = "1" }, new SelectListItem { Text = "Item 2", Value = "2", Selected = true } })
#Html.HiddenFor(model => model.DepartmentID)
Both of the above statement produce controls having the same id so i was not sure can i get the value of the dropdown in the hidden field.
I could use a different id while creating the hidden variable and assign the dropdown value it it using jquery.
I just want to know if i can achieve the same using the Hidden field having the same id as shown in the above code ??
Form Fields are not submitted by ID, they're submitted by name. It's ok for there to be two controls with the same name. However, it's invalid HTML to have two elements with the same id.
You can set a different ID but keep the same name like this:
#Html.HiddenFor(x => x.DepartmentID, new { id="DepartmentID2" })
Related
I have the below code that I use to populate my dropdown in my Razor Page
I want to preselect a description - the "Value" of that needs to be set is found in
s.UserEstablishmentId
How can I preselect this on the dropdown
#Html.DropDownList("drpEstablishments",
getEstablishments().Select(s => new SelectListItem()
{
Text = s.Description,
Value = s.EstablishId.ToString()
}),
new
{
#class = "dropdown form-control"
})
You're using linq to create a new SelectListItem for getEstablishments element. When creating each instance of a SelectListItem() you need to determine if Selected should be true or false. Simply replace YourConditionForSelectionHere with a method that returns a bool or syntax that returns a bool, shown below:
#Html.DropDownList("drpEstablishments",
getEstablishments().Select(s => new SelectListItem()
{
Selected = (YourConditionForSelectionHere),
Text = s.Description,
Value = s.EstablishId.ToString()
}),
new
{
#class = "dropdown form-control"
})
in the end something like this worked
Selected= (s.UserEstablishmentId==s.EstablishId)? true:false,
Been googling for a while, but I can't seem to get any closer.
I'm using MVC with an EF Database structure.
I want the dropdownlist items in the View to show up with different names than what they come up with.
Right now, the lambda query returns a list().
I want each item to get a string name depending on their current name. Eventually, the selected field needs to be used in another lambda as the Byte that it was.
Edit
//view
<p>
#using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<p>
#Html.DropDownList("vbType", (SelectList)ViewBag.Type, "" , new { onchange = "form.submit();" })
....
//controller
var typeLst = new List<byte>();
var typeQry = from t in db.model1
orderby t.TYPE
select t.TYPE;
typeLst.AddRange(typeQry.Distinct());
ViewBag.vbType = new SelectList(typeLst);
....
Thanks in advance.
You can create a SelectListItem to display in your dropdownlist.
Example:
#Html.DropDownListFor(model => model.YearStartedToPay,
Enumerable.Range(DateTime.Today.Year - 60, 61).OrderByDescending(x => int.Parse(x.ToString())).Select(n => new SelectListItem() { Text = n.ToString(), Value = n.ToString(), Selected = false }).ToList<SelectListItem>(),....)
In this case, I'm creating an Enumerable, that will be your list returned from EF, ordering and selecting as a SelectListItem.
You need to make use of dropdownlist's Value property. Make your list a type of SelectListItem.
Assign Text= [Your Required Text] and Value = [Current Name]
I am trying to develop a web application which I Need to save firm name username and password.
When I try to add firm username and password it gives me same firm ID in the database as;
Firmname : YAHOO INC
UserName : asd
Password : pass
İn formload all firms come but when ı try to save firmname, username and pass it gives me same firmname for all user save.
Any advice how I can resolve such issue?
Here is a simple example of how to create a drop down list in ASP.NET MVC using Html.DropDownListFor.
You can do it like this all inlined in your *.cshtml file like so:
#Html.DropDownListFor(model => model.Package.State, new SelectList(
new List<Object>{
new { value = 0 , text = "Red" },
new { value = 1 , text = "Blue" },
new { value = 2 , text = "Green"}
},
"value",
"text",
2))
With produces this:
<select id="Package_State" name="Package.State"><option value="0">Red</option>
<option value="1">Blue</option>
<option value="2">Green</option>
</select>
The model => model.Package.State expression is used to generate the id and name on the select.
The string value and text matter as they need to match the model attributes of the listItem in the list.
Or you can create your own List collection (with different fields for value and text) in your controller, set it on you model, and then use in your view like this:
#Html.DropDownListFor(model => model.Package.State.Id, new SelectList(
Model.PackageStates,
"id",
"name",
Model.Package.State.Id))
I'm trying to develop a website using asp.net mvc 4 & EF6 where I'm using dropdownlist to assign datas to my required textboxes. So far I've managed to assign data to two textboxes where one holds the value of the selected item of dropdownlist & one holds the text given for the selected item. But I need to add another value from the selected item. My codes are below,
Controller
ViewBag.Clients = new SelectList(db.ClientInfoes, "age", "fullname"); //This is given
ViewBag.Clients = new SelectList(db.ClientInfoes, "id", "age", "fullname"); //I want something like this
View
#Html.DropDownList("Clients", "Select...")
// Textboxes are populated automatically by using jQuery
#Html.TextBoxFor(a => a.Fullname, new { id = "clientName", #readonly = "readonly" })
#Html.TextBoxFor(a => a.Age, new { id = "clientAge", #readonly = "readonly" })
How can I assign more than one value in the SelectList? Any way I can do that would be fine. Need this help badly. Thanks.
It looks like you want to show name and age both in the dropdown text, in that case you need to project the result coming from database :
var ClientInfoes = db.ClientInfoes
.Select(x=>
new {
id = x.id,
name = x.fullname+","+x.age
});
ViewBag.Clients = new SelectList(ClientInfoes, "id", "name");
I'm setting up a project with Kendo Ui for the first time, and I'm trying to create Kendo UI dropdown lists, but am getting:
ArgumentNullException was unhandled by user code.
--Value cannot be null.
My code is:
#(Html.Kendo().DropDownList()
.DataTextField("Text")
.DataValueField("Value")
.BindTo(new List<SelectListItem>()
{
new SelectListItem()
{
Text = "Active",
Value = "1"
},
new SelectListItem()
{
Text = "In-active",
Value = "2"
}
})
.Value("1")
) <-- the error is pointing to here.
Does anyone know what value is missing?
Thanks,
Each KendoUI component must have a name specified.
Add this:
.Name("whatYouWantForHtmlId")