make dropdown list of person - asp.net-mvc-3

i have a model :
public class person
{
public int id{get;set;}
public string name{get;set;}
}
how can i make a drop down list, from list of person in mvc3 razor by this syntax : #Html.DropDownListFor(...) ?
what type must be my persons list?
sorry I'm new in mvc3
thanks all

public class PersonModel
{
public int SelectedPersonId { get; set; }
public IEnumerable<Person> persons{ get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
then in the controller
public ActionResult Index()
{
var model = new PersonModel{
persons= Enumerable.Range(1,10).Select(x=>new Person{
Id=(x+1),
Name="Person"+(x+1)
}).ToList() <--- here is the edit
};
return View(model);//make a strongly typed view
}
your view should look like this
#model Namespace.Models.PersonModel
<div>
#Html.DropDownListFor(x=>x.SelectedPersonId,new SelectList(Model.persons,"Id","Name","--Select--"))
</div>

You should translate that to a List<SelectListItem> if you want to use the build in MVC HtmlHelpers.
#Html.DropDownFor(x => x.SelectedPerson, Model.PersonList)
Alternatively, you can simply make your own in the template:
<select id="select" name="select">
#foreach(var item in Model.PersonList)
{
<option value="#item.id">#item.name</option>
}
</select>

Related

While filtering, how to POST a SearchModel, but GET a different ResultsModel back in the view

I am creating a filter view to find records. This example on SO helps, but does not mention how handle the (Filtered) View.
The err below is because, the actions returns a List<ProductViewModel>, and it Errors/complains that the View is using a SearchViewModel, I need to this POST the searchmodel/variables, butGET back the list/results model
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[ViewModels.ProductVM]', but this
dictionary requires a model item of type 'ViewModels.SearchModel'.
Issue/Question: Since there are two models, the SearchViewModel passed to the controller & the ProductViewModel returned as a result, which model should be strongly typed to the view? and How can I create the view to handle both SearchModel & ProductModel If I stronglyType ProductVM, then I loose the submitform from the SearchVM.
I create the SearchView as the mainview, & the _ResultsPartialView as a partialView, is this wrong?
public ActionResult Index(SearchModel searchModel)
{
var filteredProdVMList = _Repository.GetFilteredProducts(searchModel);
return View(filteredProdVMList);
}
public class ProductVM
{
public int Id { get; set; }
public int Price { get; set; }
public string Name { get; set; }
// implicit const... blah.. removed
}
public class SearchModel
{
public int? Id { get; set; }
public int? PriceFrom { get; set; }
public int? PriceTo { get; set; }
public string Name { get; set; }
}
You need to modify your SearchModel to include a collection property for the products
public class SearchModel
{
public int? PriceFrom { get; set; }
public int? PriceTo { get; set; }
....
public IEnumerable<ProductVM> Products { get; set; } // add
}
then you return just SearchModel to your view
public ActionResult Filter(SearchModel filter)
{
filter.Products = _repository.GetFilteredProducts(filter);
return View(filter);
}
and your view will be
#model SearchModel
....
#using (Html.BeginForm("Filter", "yourControllerName", FormMethod.Get))
{
#Html.LabelFor(m => m.PriceFrom)
#Html.EditorFor(m => m.PriceFrom)
#Html.ValidationMessageFor(m => m.PriceFrom)
... // other form controls for properties you want to filter the results on
<input type="submit" value="Filter" />
}
#Html.Partial("_ResultsPartialView", Model.Products)

ASP.NET Core MVC Entity Framework binding for sub entities

Let's suppose I have these 2 entities:
public class Person
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long id_person {get;set;}
[Column(TypeName = "varchar(255)")]
[StringLength(255)]
public String name {get;set;}
}
public class InterestCenter
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long id_interest {get;set;}
[Column(TypeName = "varchar(255)")]
[StringLength(255)]
public String name {get;set;}
}
I want to set a many to many relationship between this 2 entities. This mean a Person can have many interest centers.
Here is what I've done:
public class PersonHasInterestCenter
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long id {get;set;}
[ForeignKey("person")]
public long id_person {get;set;}
public virtual Person person { get; set; }
[ForeignKey("interestcenter")]
public long id_interest {get;set;}
public virtual InterestCenter interestcenter { get; set; }
}
Now I want to create a controller action and cshtml razor view in order to edit and save a Person. What I want to do is to display a set of checkboxes with all available interst centers.
Here is what I've done:
[HttpPost]
public async Task<IActionResult> MyAction(long id, [Bind("id_person,name")] Person p)
{
ViewBag.interestcenters = mydbcontext.interestcenters;
ViewBag.message = "";
if (p.name == "")
{
ViewBag.message = "You need to set name.";
}
else if (ModelState.IsValid == false)
{
ViewBag.message = string.Join("; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
}
else
{
mydb_context.Update(p);
await mydb_context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(p);
}
And here is the associated cshtml razor view:
#model myproject.Person
<form asp-action="MyAction">
<div>#ViewBag.message</div>
<input type="hidden" asp-for="id_person" />
<input asp-for="name" />
#foreach (var name in ViewBag.interestcenters)
{
<input type="checkbox" asp-for="WHAT_SHOULD_I_PUT_THERE" />#item.name
}
<input type="submit">
</form>
Everything works great for create or update person's name but I have a problem with interest center checkboxes. I have also tried to create a view model. But I do not manage to make it work...
Thanks for your help
The simple answer is ... use viewmodels whenever have the chance.
In order to bind those interests you could create something similar to:
public class MyViewModel
{
public long UserId { get; set; }
public List<InterestCenterViewModel> InterestCenters { get; set; }
}
public class InterestCenterViewModel
{
public int Id { get; set; }
public bool IsSelected { get; set; }
public string Name { get; set; }
}
On the get method:
[HttpGet]
public async Task<IActionResult> MyAction(long id)
{
var _InterestCenters = mydbcontext.interestcenters;
// Create your vm here
var model = new MyViewModel
{
UserId = id,
InterestCenters = _InterestCenters.Select(p => new InterestCenterViewModel
{
Id = p.Id,
Name = p.Name
IsSelected = false
}).ToList()
}
return View(model);
}
On your post:
[HttpPost]
public async Task<IActionResult> MyAction(MyViewModel model)
{
// Something was not filled or did not match your requirements
if (!ViewState.IsValid)
{
return View(model);
}
// All good. To your stff here
return Ok();
}
So all you need is to pass the list of interests created using the above model to the view:
<input asp-for="UserId" type="hidden" />
#for(int i = 0; i < MyViewModel.InterestCenters.Count; i++)
{
<input type="checkbox" asp-for="MyViewModel.InterestCenters[i].IsSelected" />#MyViewModel.InterestCenters[i].Name
}
When working with lists you need to use for instead of foreach. The html generated is using that index i instead of the name which is the way to make the difference between items.
Here is a link describing how viewmodels and asp.net works: Microsoft official documentation

ASP.NET MVC 3 dropdown binding

I encountered the next indexer syntax during binding my model with collection to view.
Here is what I have:
public class CustomerModel
{
public List<Customer> Customers { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public ImportAction ImportAction { get; set; }
}
public enum ImportAction
{
Skip,
Add,
Merge
}
My view:
#using (Html.BeginForm("Edit", "Home"))
{
var index = 0;
foreach (var customer in Model.Customers)
{
<span>Name: #customer.Name</span>
#Html.DropDownListFor(x => x.Customers[index].ImportAction, customer.ImportAction.ToListItems())
index++;
}
<button type="submit">
Submit</button>
}
How to avoid this [index] usage? Any other correct syntax? Take to the look, that without it #Html.DropDownListFor would not work and update my model on post back.
you can use the loop variable 'customer' like the following:
#Html.DropDownListFor(x => customer.ImportAction)

MVC Model binding with lists

I have this model:
public class ReservationViewModel
{
public Flight InboundFlight { get; set; }
public Flight OutboundFlight { get; set; }
}
//Flight
public class Flight
{
public List<ISeat> Seats { get; set; }
}
//Seats
public interface ISeat
{
ECabin Cabin { get; }
int NumberOfSeatsAvailable { get; }
int SeatsChosen { get; set; }
string PropertyName { get; }
}
My HTML consist of the folliwing:
<select id="OutboundFlight__0__Seats_SeatsChosen" name="OutboundFlight.[0].Seats.SeatsChosen" class="valid"><option...
<select id="OutboundFlight__0__Seats_SeatsChosen" name="OutboundFlight.[1].Seats.SeatsChosen" class="valid"><option...
<select id="OutboundFlight__0__Seats_SeatsChosen" name="OutboundFlight.[2].Seats.SeatsChosen" class="valid"><option...
My Action:
[HttpPost]
public ActionResult Index(ReservationViewModel model, FormCollection form)
{
return View();
}
Upon submit I try to bind back to the model but the Seats of each flight returns null...
Help will be appreciated
The HTML being generated is incorrect to get it to bind to a list - the field name has to match what what accessing the property from c# would look like:
This should work:
name="OutboundFlight.Seats[0].SeatsChosen"

ViewBag multiple SelectList for dropdown list

I am trying to use dropdownList with two foreign keys which are modelId, and categoryId.
And I am using ViewBag with selectList.
public ActionResult Create()
{
ViewBag.categoryId = new SelectList(db.Category, "categoryId", "name");
ViewBag.modelId = new SelectList(db.Model, "modelId", "name");
return View();
}
//
// POST: /Product/Create
[HttpPost]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.Product.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.categoryId = new SelectList(db.Category, "categoryId", "name", product.categoryId);
ViewBag.modelId = new SelectList(db.Model, "modelId", "name", product.modelId);
return View(product);
}
And here is my Create.cshtml.
<div class="editor-label">
#Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">
#Html.DropDownList("categoryId", "--Select--")
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Model)
</div>
<div class="editor-field">
#Html.DropDownList("modelId", "--Select--")
</div>
When I press submit button, error come up,
'An item with the same key has already been added'
What is problem? Is it problem with in Model?
Here is my models.
--Prodruct.cs--
public class Product
{
[Key] public int productId { get; set; }
[Required(ErrorMessage = "Please select category")]
public int categoryId { get; set; }
[Required(ErrorMessage = "Please select model")]
public int modelId { get; set; }
[DisplayName("Model name")]
public String model { get; set; }
public virtual Category Category { get; set; }
public virtual Model Model { get; set; }
}
--Category.cs--
public class Category
{
[Key] public int categoryId { get; set; }
public String name { get; set; }
}
--Model.cs--
public class Model
{
[Key] public int modelId { get; set; }
public String name { get; set; }
}
--RentalDB.cs--
public class rentalDB : DbContext
{
public DbSet<Product> Product { get; set; }
public DbSet<Model> Model { get; set; }
public DbSet<Customer> Customer { get; set; }
public DbSet<Order> Order { get; set; }
public DbSet<Cart> Cart { get; set; }
public DbSet<Category> Category { get; set; }
public DbSet<OrderDetails> OrderDetails { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Where it is wrong? Index page in Create can get category data and model data. However, when I submit it, it has error, 'An item with the same key has already been added'.
Could you help me where has got problem?
Thank you.
--added more coding--
I am using this LINQ. Probably here has problem.
How can I add 'Model' entity in here?
var product = from a in db.Product.Include(a => a.Category)
select a;
This is how I would have done it..
I would suggest that you don't send your domain models to the view, but rather create a view model for each view. Doing it this way you will only include what is needed on the screen.
Create a new view model for your Create view:
public class ProductCreateViewModel
{
// Include other properties if needed, these are just for demo purposes
public string Name { get; set; }
public string SKU { get; set; }
public string LongDescription { get; set; }
// This is the unique identifier of your category,
// i.e. foreign key in your product table
public int CategoryId { get; set; }
// This is a list of all your categories populated from your category table
public IEnumerable<Category> Categories { get; set; }
// This is the unique identifier of your model,
// i.e. foreign key in your product table
public int ModelId { get; set; }
// This is a list of all your models populated from your model table
public IEnumerable<Model> Models { get; set; }
}
Category class:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
Model class:
public class Model
{
public int Id { get; set; }
public string Name { get; set; }
}
In your Create view you would have the following:
#model MyProject.ViewModels.ProductCreateViewModel
#using (Html.BeginForm())
{
<table>
<tr>
<td><b>Category:</b></td>
<td>
#Html.DropDownListFor(x => x.CategoryId,
new SelectList(Model.Categories, "Id", "Name", Model.CategoryId),
"-- Select --"
)
#Html.ValidationMessageFor(x => x.CategoryId)
</td>
</tr>
<tr>
<td><b>Model:</b></td>
<td>
#Html.DropDownListFor(x => x.ModelId,
new SelectList(Model.Models, "Id", "Name", Model.ModelId),
"-- Select --"
)
#Html.ValidationMessageFor(x => x.ModelId)
</td>
</tr>
</table>
<!-- Add other HTML controls if required and your submit button -->
}
Your Create action methods:
public ActionResult Create()
{
ProductCreateViewModel viewModel = new ProductCreateViewModel
{
// Here you do database calls to populate your dropdowns
Categories = categoryService.GetAllCategories(),
Models = modelService.GetAllModels()
};
return View(viewModel);
}
[HttpPost]
public ActionResult Create(ProductCreateViewModel viewModel)
{
// Check that viewModel is not null
if (!ModelState.IsValid)
{
viewModel.Categories = categoryService.GetAllCategories();
viewModel.Models = modelService.GetAllModels();
return View(viewModel);
}
// Mapping
Product product = ... // Do your mapping here
// Insert product in database
productService.Insert(product);
// Return the view where you need to be
}
I would also recommend that you use AutoMapper to do the mappings for you between your domain model and view model. I would also recommend that you look at Fluent Validation to take care of your view model validations.
I hope this helps.
UPDATED ANSWER
The service that was used to get all the categories could look like this:
public class CategoryService : ICategoryService
{
private readonly ICategoryRepository categoryRepository;
public CategoryService(ICategoryRepository categoryRepository)
{
// Check if category repository is not null, throw exception if it is
this.categoryRepository = categoryRepository;
}
public IEnumerable<Category> GetAllCategories()
{
return categoryRepository.GetAllCategories();
}
}
categoryRepository is injected by Autofac.
Category service interface:
public interface ICategoryService
{
IEnumerable<Category> GetAllCategories();
}
I currently still use Entity Framework 4.1 code first.
My category repository:
public class CategoryRepository : ICategoryRepository
{
MyContext db = new MyContext();
public IEnumerable<Category> GetAllCategories()
{
return db.Categories
.OrderBy(x => x.Name);
}
}
My category repository interface:
public interface ICategoryRepository
{
IEnumerable<Category> GetAllCategories()
}
public class Test
{
rentalDB db = new rentalDB();
public Product LoadProductById(int pId)
{
return db.Products.Include(p => p.Model).Include(p => p.Category).Where(p => p.productId == pId).SingleOrDefault();
} // To get specific product.
public IEnumerable<Product> LoadAllProducts()
{
return db.Products.Include(p => p.Model).Include(p => p.Category).ToList();
} // To get all products.
}
I have changed your DbSet to Products make it more clear. This is how you load all the references for one product or all products, in order to iterate over them.

Resources