finding out the firstrow values of <SelectListItem> in the controller before sending them to view MVC3? - asp.net-mvc-3

Im binding UsersList to a dropdownlist using a selectlistitem
#Html.DropDownListFor(m => m.UserId, new SelectList(Model.Users, "Value", "Text", Model.UserId))
public List<SelectListItem> Users { get; set; }
Public ActionResult LoadUsers()
{
UserModel usrModel=new UserModel();
usrModel.LoadUsers();
string firstUser=?
return view(usrModel);
}
Im binding UsersList to a dropdownlist using a selectlistitem
my requirement is when the user is sensing the Users to view from controller, i want to find out the value field of the User in first row, i.e before binding to the dropdownlist itself i want to find out who is the first User who will be selected in the dropdownlist when the page first loads

Related

Cannot Populate TextBox in MVC

public IActionResult Create() { ViewData["StateId"] = new SelectList(_context.States, "Id", "Abbreviation")
ViewData["UserId"] = Guid.NewGuid().ToString(); return View(); }
I have the above code where I need to populate a dropdown list with some States e.g. Alabama etc.
Then I need to populate a textbox with a UserID(new Guid). This code is in my controller in the part that a new user is created. The code populates the dropdown list but not the textbox.
Any idea why?

Binding dynamic data to kendo Dropdownlist

I am trying to bind a kendo dropdownlist with the model but I am getting undefined in each option.
Model:
public SelectList CountriesTemp { get; set; }
public int Country{get;set;}
Controller:
public ActionResult Registration()
{
RegistrationModel Model = new RegistrationModel();
Model.CountriesTemp = new SelectList(ObjService.GetCountries(), "CountryID", "Country_Name");
return View(Model);
}
View Page
#(Html.Kendo().DropDownListFor(m => m.Country)
//The name of the dropdownlist is mandatory. It specifies the "id" attribute of the widget.
.DataTextField("Country_Name") //Specifies which property of the Product to be used by the dropdownlist as a text.
.DataValueField("CountryID") //Specifies which property of the Product to be used by the dropdownlist as a value.
.BindTo(Model.CountriesTemp ) //Pass the list of Products to the dropdownlist.
)
Can somebody please guide me where I am wrong because if I bind a simple dropdownlist of MVC , It works well. Just one line change as below in ViewPage and it's Working.
#Html.Dropdownlist("CountriesTemp")
It looks like you've got a conflict (so-to-speak going on here). Try something like this:
#(Html.Kendo().DropDownListFor(m => m.Country)
.DataTextField("Text")
.DataValueField("Value") // These may be optional now
.BindTo(Model.CountriesTemp))
Alternatively, you can not use a SelectList and do a property like IEnumerable<Country> Countries { get; set; } on your Model. Then the binding would look something like:
#(Html.Kendo().DropDownListFor(m => m.Country)
.DataTextField("Country_Name")
.DataValueField("CountryID")
.BindTo(Model.Countries))

MVC dropdownlist , data not displaying when model is not Ienumerable

I am having a problem passing to the View a Model that contains the dropdown data and the model.
With this code my page loads, but the dropdownlist contains "System.Web.MVC.SelectList" when selected.
Here's my controller code.
public ActionResult Index(string productNameFilter, string productCategoryFilter, String productTypeFilter )
{
var ddl = new Items();
ddl.CategoryddList = itemsRepository.GetItemDdl("Item Categories").Select(c => new SelectListItem
{
Value = c.DropdownID.ToString(),
Text = c.DropdownText
});
ViewBag.CategoryDD = new SelectList(ddl.CategoryddList, "Value", "Text");
var model = itemsRepository.GetItemByName(productNameFilter);
return View(model);
}
Here's my view
#model Ienumerable<Models.items.items>
#Html.DropDownList("productCategoryFilter",
new SelectList(ViewBag.CategoryDD),
"---Select Category---")
Side note - if you use a ViewModel between the View and the Model instead of binding directly to the model, you can put your SelectList on the ViewModel and use #Html.DropdownFor() instead of #Html.Dropdown(). The ViewBag should really be used sparingly.
However back to your original question:
What is "Items()"? in your line
var ddl = new Items();
I'm not sure what good reason you would have NOT to make it enumerable.
I suspect it is not working because you are making a selectlist from a select list twice --
in your code behind you are defining ViewBag.CategoryDD as a SelectList(), and then in your Razor code you are creating a new SelectList() from the existing selectlist. You shouldn't have to do this.
The way I would do this is create a ProductViewModel class that contains your product category list AND your list of products (your current model), and a property for the selected filter.
public class ProductViewModel
{
public IEnumerable<Model.items.items> ProductList {get;set;}
public IEnumerable<SelectListItem> ProductCategoryList {get;set;} //SelectList is an IEnumerable<SelectListItem>
public string SelectedCategory {get;set;}
}
Then on your view the model would be
#model ProductViewModel
#Html.DisplayFor(model => model.SelectedCategory, "---Select Category---")
#Html.DropdownListFor(model => model.SelectedCategory, Model.ProductCatgoryList)

to set dropdownlist value in view when record is extracted in mvc

working with MVC 3.
I have a dropdownlist which populates Gender with value 'M' for text 'Male' and 'F' for text 'Female'.
Now, when I extract record it is not setting the value as per the record's value.
I have following on code in view.
#if(Model != null)
{
#Html.DropDownListFor(model => model.GENDER,
new SelectList(ViewBag.gender, "Value", "key", Model.GENDER))
}
else
{
#Html.DropDownListFor(model => model.GENDER,
new SelectList(ViewBag.gender, "Value", "Key"))
}
The above code means that when page first loads it has no value for gender field as model is empty so just populate list and when person is extracted i.e. when model is not null it populates and set value equal to model.gender. But it is not setting the value. What could be the problem?
Are you passing an NULL object (your model) to your view ? How many places in the view are you going to write #if(Model != null).. . Is that not going to mess up the concept of Clean code. ?
I guess this is how you should do it. You should always pass the model/ Viewmodel to the view no matter it is the firs time (Create Entity) or the Editing time. Some thing like this
public ActionResult Create()
{
CustomerViewModel model=new CustomerViewModel();
return View(model);
}
For Edit action, You fill the Model/ViewModel object filled with data.
public ActionResult Edit(int id)
{
CustomerViewModel model=new CustomerViewModel();
model=CustomerService.GetCustomerFromId(id);
return View(model);
}
and in the View, you use it like this
#model CustomerViewModel
#Html.DropDownListFor(model => model.GENDER,
new SelectList(ViewBag.gender, "Value", "key", Model.GENDER))

Read The Data From DropDown in ASP.Net MVC3

I am doing the ASP.net MVC 3 (Empty type and not the internet type) with the Database First approach...
What i need is
Step 1:
I just used the dropdown to display the various locations where the company is located. The list comes from the Organization table and Location is only one string field in this Oranization Table,
Step 2:
While the user is doing registration, the dropdown list will show the locations.. Now, user selects India, then this value (Location Name) should store in the UserLogin Table...
Now how to read the value from the dropdown and i hope you understand my question and thanks in advance
I would use view models:
public class RegisterViewModel
{
public string LocationName { get; set; }
public IEnumerable<SelectListItem> Locations { get; set; }
}
then a controller action that will serve the view:
public ActionResult Index()
{
var model = new RegisterViewModel();
model.Locations = new SelectList(dbcontext.Organization_Details, "OName", "OLocation");
return View(model);
}
then the corresponding strongly typed view:
#model RegisterViewModel
#using (Html.BeginForm())
{
#Html.LabelFor(x => x.LocationName)
#Html.DropDownListFor(x => x.LocationName, Model.Locations)
<button type="submit">OK</button>
}
and finally the controller action that will be invoked when the form is submitted:
[HttpPost]
public ActionResult Index(RegisterViewModel model)
{
// model.LocationName will contain the selected location here
...
}

Resources