I am developing an ASP.Net MVC 3 Web application and I am having difficulties retrieving the selected checkbox values within the HttpPost method in my Controller. Hopefully someone can help.
I have 2 ViewModels
public class ViewModelShiftSubSpecialties
{
public IEnumerable<ViewModelCheckBox> SpecialtyList { get; set; }
}
public class ViewModelCheckBox
{
public string Id { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
public string Specialty { get; set; }
}
And a partial View I use as an EditorTemplate
#model Locum.UI.ViewModels.ViewModelCheckBox
#Html.HiddenFor(x => x.Id)
#Html.CheckBoxFor(x => x.Checked)
#Html.LabelFor(x => x.Name, Model.Name)<br />
In my View I create the checkboxes under two headings, Medicine and Surgery
<h3>Medicine</h3>
foreach (var sub in Model.SpecialtyList)
{
if (sub.Specialty.Equals("Medicine"))
{
#Html.EditorFor(m => sub)
}
}
<h3>Surgery</h3>
foreach (var sub in Model.SpecialtyList)
{
if (sub.Specialty.Equals("Surgery"))
{
#Html.EditorFor(m => sub)
}
}
And then in my HttpPost Controller I try to get the values of the selected checkboxes, but mode.SpecialtyList is always Null
[HttpPost]
public ActionResult AssignSubSpecialties(ViewModelShiftSubSpecialties model)
{
foreach (var item in model.SpecialtyList)
{
if (item.Checked)
{
//do some logic
}
}
return View();
}
Does anyone know why model.SpecialtyList is always Null?
Any help is much appreciated.
Thanks.
give checkboxes same names like:
<input type="checkbox" name="ViewModelShiftSubSpecialties.SpecialtyList" .../>
and it will post an array
Related
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
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)
I'm new to MVC, and stuck on what should be a pretty straight forward issue. I'm working through this tutorial and got everything pretty much working, except I now want to add a foreign key 'link' (not sure what it's called) but can't seem to get it to work. Here's what I have:
Tables:
Inventory:
Id | SerialNumber | ManufacturerId (foreignkey to Manufactueres->id)
Manufactureres
Id (primary key) | Name
Model (InventoryItem.cs):
public class InventoryItem {
public int Id {get; set; }
public int SerialNumber{ get; set; }
//this starts the trouble, I actually want to interact with the Manufactureres table -> Name column
public int ManufacturerId { get; set; }
}
View (Create.cshtml):
...
//What i really want is a dropdown of the values in the Manufactureres table
#Html.EditorFor(model=> model.ManufacturerId)
This must be a farely common issue when using a relational database there would be many foreign key relationships to be used/shown, but for some reason i can't find a tutorial or issue on stackoverflow that directly corresponds to something so simple. Any guidance, or direction is much appreciated!
Thanks,
I hope I understand your question correctly. Seems like when you want to add a new inventory item then you want a list of all the manufacturers in a dropdown list. I am going to work on this assumption, please let me know if I am off the track :)
Firstly go and create a view model. This view model you will bind to yout view. Never bind domain objects to your view.
public class InventoryItemViewModel
{
public int SerialNumber { get; set; }
public int ManufacturerId { get; set; }
public IEnumerable<Manufacturer> Manufacturers { get; set; }
}
Your domain objects:
public class InventoryItem
{
public int Id { get; set; }
public int SerialNumber{ get; set; }
public int ManufacturerId { get; set; }
}
public class Manufacturer
{
public int Id { get; set; }
public string Name { get; set; }
}
Your controller might look like this:
public class InventoryItemController : Controller
{
private readonly IManufacturerRepository manufacturerRepository;
private readonly IInventoryItemRepository inventoryItemRepository;
public InventoryItem(IManufacturerRepository manufacturerRepository, IManufacturerRepository manufacturerRepository)
{
// Check that manufacturerRepository and inventoryItem are not null
this.manufacturerRepository = manufacturerRepository;
this.inventoryItemRepository = inventoryItemRepository;
}
public ActionResult Create()
{
InventoryItemViewModel viewModel = new InventoryItemViewModel
{
Manufacturers = manufacturerRepository.GetAll()
};
return View(viewModel);
}
[HttpPost]
public ActionResult Create(InventoryItemViewModel viewModel)
{
// Check that viewModel is not null
if (!ModelState.IsValid)
{
Manufacturers = manufacturerRepository.GetAll()
return View(viewModel);
}
// All validation is cool
// Use a mapping tool like AutoMapper
// to map between view model and domain model
InventoryItem inventoryItem = Mapper.Map<InventoryItem>(viewModel);
inventoryItemRepository.Insert(inventoryItem);
// Return to which ever view you need to display
return View("List");
}
}
And then in your view you might have the following:
#model MyProject.DomainModel.ViewModels.InventoryItems.InventoryItemViewModel
<table>
<tr>
<td class="edit-label">Serial Number <span class="required">**</span></td>
<td>#Html.TextBoxFor(x => x.SerialNumber, new { maxlength = "10" })
#Html.ValidationMessageFor(x => x.SerialNumber)
</td>
</tr>
<tr>
<td class="edit-label">Manufacturer <span class="required">**</span></td>
<td>
#Html.DropDownListFor(
x => x.ManufacturerId,
new SelectList(Model.Manufacturers, "Id", "Name", Model.ManufacturerId),
"-- Select --"
)
#Html.ValidationMessageFor(x => x.ManufacturerId)
</td>
</tr>
</table>
I hope this helps :)
Yes, this is common issue, you need select Manufactureres in action and then send them to view. You can use ViewBag or strontly typed view model.
Examples:
Problem populating dropdown boxes in an ASP.NET MVC 3
Application
Having difficulty using an ASP.NET MVC ViewBag and
DropDownListfor
MVC3 Razor #Html.DropDownListFor
This is what I would recommend you.
1) Create a Manufacturer model class
public class Manufacturer
{
public int Id { get; set; }
public string Name { get; set; }
}
2) Create InventoryItem model class as follows
public class InventoryItem
{
public int Id { get; set; }
public int SerialNumber{ get; set; }
public int ManufacturerId { get; set; }
[ForeignKey("Id ")]
public Manufacturer Manufacturer{get; set;}
public IEnumerable<Manufacturer> Manufacturer {get;set;}
}
3) Make sure DbContext is also updated as follows
public DbSet<InventoryItem> InventoryItem {get;set;}
public DbSet<Manufacturer> Manufacturer{get;set;}
4) Controller
[HttpGet]
public ActionResult Create()
{
InventoryItem model = new InventoryItem();
using (ApplicationDbContext db = new ApplicationDbContext())
{
model.Manufacturer= new SelectList(db.Manufacturer.ToList(), "Id", "Name");
}
return View(model);
}
[HttpPost]
public ActionResult Create(InventoryItem model)
{
//Check the Model State
if(! ModelState.IsValid)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
model.Manufacturer= new SelectList(db.Manufacturer.ToList(), "Id", "Name");
return View(model);
}
}
using (ApplicationDbContext db = new ApplicationDbContext())
{
InventoryItem dto = new InventoryItem();
dto.SerialNumber= model.SerialNumber;
dto.Id= model.Id;
Manufacturer manudto = db.Manufacturer.FirstOrDefault(x => x.Id== model.Id);
dto.CatName = manudto.CatName;
db.Test.Add(dto);
db.SaveChanges();
}
TempData["SM"] = "Added";
return RedirectToAction("Index");
}
5) Make sure View has dropdownselect option in below format
<div class="form-group">
#Html.LabelFor(model => model.Name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.Id, Model.Manufacturer,"Select", new { #class = "form-control" } )
#Html.ValidationMessageFor(model => model.Id, "", new { #class = "text-danger" })
</div>
</div>
Hope this works :D
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.
I need to populate a dropdown in ASP.NET MVC 3. I was hoping to get the answers to the following questions:
What options do I have. I mean what's the difference between #Html.DropDownList and #Html.DropDownListFor. Also are there any other options?
I have the following classes/code. What would be the razor code/HTML syntax I need (I have included what I was trying) in my .cshtml assuming I need to only show this dropdown using the classes below.
public class SearchCriterion
{
public int Id { get; set; }
public string Text { get; set; }
public string Value { get; set; }
}
public class SearchCriteriaViewModel
{
public IEnumerable<SelectListItem> SearchCriteria { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index()
{
IList<System.Web.WebPages.Html.SelectListItem> searchCriteriaSelectList =
new List<System.Web.WebPages.Html.SelectListItem>();
SearchCriteriaViewModel model = new SearchCriteriaViewModel();
//Some code to populate searchCriteriaSelectList goes here....
model.SearchCriteria = searchCriteriaSelectList;
return View(model);
}
}
//Code for Index.cshtml
#model SearchCriteriaViewModel
#{
ViewBag.Title = "Index";
}
<p>
#Html.DropDownListFor(model => model.SearchCriteria,
new SelectList(SearchCriteria, "Value", "Text"))
</p>
the right side of the lambda => has to be a simple type not the complex type modify your code like
public class SearchCriteriaViewModel
{
public int Id { get; set; }
public IEnumerable<SearchCriterion> SearchCriteria { get; set; }
}
public class SearchCriterion
{
public string Text { get; set; }
public string Value { get; set; }
}
the controller will look like
public ActionResult Index()
{
//fill the select list
IEnumerable<SearchCriteria> searchCriteriaSelectList =
Enumerable.Range(1,5).Select(x=>new SearchCriteria{
Text= x.ToString(),
Value=ToString(),
});
SearchCriteriaViewModel model = new SearchCriteriaViewModel();
//Some code to populate searchCriteriaSelectList goes here....
model.SearchCriteria = searchCriteriaSelectList;
return View(model);
}
in the view
#model SearchCriteriaViewModel
#{
ViewBag.Title = "Index";
}
<p>
#Html.DropDownListFor(model => model.Id,
new SelectList(model.SearchCriteria , "Value", "Text"))
</p>
After extensively using ASP.NET MVC for the past 3 years, I prefer using additionalViewData from the Html.EditorFor() method more.
Pass in your [List Items] as an anonymous object with the same property name as the Model's property into the Html.EditorFor() method.
The benefit is that Html.EditorFor() method automatically uses your Editor Templates.
So you don't need to provide CSS class names for your Drop Down Lists.
See comparison below.
//------------------------------
// additionalViewData <=== RECOMMENDED APPROACH
//------------------------------
#Html.EditorFor(
m => m.MyPropertyName,
new { MyPropertyName = Model.ListItemsForMyPropertyName }
)
//------------------------------
// traditional approach requires to pass your own HTML attributes
//------------------------------
#Html.DropDown(
"MyPropertyName",
Model.ListItemsForMyPropertyName,
new Dictionary<string, object> {
{ "class", "myDropDownCssClass" }
}
);
//------------------------------
// DropDownListFor still requires you to pass in your own HTML attributes
//------------------------------
#Html.DropDownListFor(
m => m.MyPropertyName,
Model.ListItemsForMyPropertyName,
new Dictionary<string, object> {
{ "class", "myDropDownCssClass" }
}
);
If you want more details, please refer to my answer in another thread here.