I am tryingto populate a DropDownlist with the viewbag defined in the controller and i used a modal popup :
ProductController.cs
public IActionResult Create()
{
List<Category> categories = _dbcontext.Category.ToList();
ViewBag.bpCategories = new SelectList(categories, "CategoryId", "CategoryName");
Product product = new Product();
return PartialView("_AddProductPartialView",product);
}
_AddProductPartialView.cshtml:
#model WebApplication1.Models.Product
<div class="modal fade" role="dialog" tabindex="-1" id="addProduct" aria-labelledby="addProductLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addProductLabel">Products</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
......
<div class="form-group">
<label asp-for="CategoryId" class="control-label">CategoryId</label>
#Html.DropDownListFor(m => m.CategoryId, ViewBag.bpCategories as IEnumerable<SelectListItem>, "--Select ---", new { #class = "form-control" })
</div>
...
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
But i got this error:
The ViewData item that has the key 'CategoryId' is of type
'System.Int32' but must be of type 'IEnumerable'.
So what is the correct way to populate a DropDownlist in a modal popup??
EDIT
I tried also this code but i got an empty Dropdownlist:
<select asp-for="CategoryId" class="form-control" asp-items="ViewBag.bpCategories"></select>
Following code snippet with testing data can populate dropdown well, you can refer to it.
List<Category> categories = new List<Category>
{
new Category
{
CategoryId = 1,
CategoryName = "Category1"
},
new Category
{
CategoryId = 2,
CategoryName = "Category2"
},
new Category
{
CategoryId = 3,
CategoryName = "Category3"
}
};
ViewBag.bpCategories = categories.AsQueryable().Select(s => new SelectListItem { Value = s.CategoryId.ToString(), Text = s.CategoryName }).ToList();
In view page
<select asp-for="CategoryId" class="form-control" asp-items="ViewBag.bpCategories as List<SelectListItem>"></select>
Test Result
In Controller :
List<CollectionDetails> CollectionData = new List<CollectionDetails>();
CollectionData = GetCollectionDetails();
TempData["Department"] = new SelectList(CollectionData, "CollectionId",
"CollectionName");
In ViewModal :
#Html.DropDownListFor(model => model.CollectionId, new SelectList(TempData["Department"] as System.Collections.IEnumerable, "value", "text"), ---please select---, new { #tabindex = 13, #class = "form-control" })
Related
I am working on a ASP.NET Core CRUD applciation using modal popup i have a master detail models Stock and Article. i used this code to display the modal popup:
StockController:
public IActionResult Index()
{
List<Category> categories = _dbcontext.Category.ToList();
ViewBag.ListCategories = new SelectList(categories, "CategoryId", "CategoryName");
List<Stock> AllStocks = _dbcontext.Stock.ToList();
return View(AllStocks);
}
[HttpGet]
public IActionResult Create()
{
Stock stock = new Stock();
stock.Articles.Add(new Article() { ArticleId = 1 });
return View("_AddStockPartialView", stock);
}
[HttpPost]
public IActionResult Create(Stock stock)
{
if (stock != null)
{
_dbcontext.Stock.Add(stock);
_dbcontext.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
Index.cshtml:
#model IEnumerable<Stock>
#{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Theme.cshtml";
}
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Stock</h3>
<div class="card-tools">
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#addStock" onclick="GetDetails()">
<i class="fa fa-plus"></i>
Ajouter
</button>
</div>
</div>
<div class="card-body" id="display">
<table class="table table-bordered table-hover">
.....
</table>
</div>
</div>
</div>
</div>
<script>
function GetDetails() {
$.ajax({
type: "Get",
url: "/Stock/Create",
success: function (res) {
$("#display").html(res);
$("#addStock").modal('show');
}
});
}
</script>
_AddStockPartialView.cshtml:
#model GestionStock.Models.Stock
#{
ViewData["Title"] = "_AddStockPartialView";
}
<div class="modal fade " role="dialog" tabindex="-1" id="addStock" aria-labelledby="addStockLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header">
<h5>Stock</h5>
</div>
<div class="modal-body">
<form asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CategoryId" class="control-label"></label>
<select asp-for="CategoryId" class="form-control" asp-items="ViewBag.ListCategories"></select>
<span asp-validation-for="CategoryId" class="text-danger"></span>
</div>
.......
<table class="table table-striped" id="articleTable">
<thead>
<tr>
<th>Numero serie</th>
<th>Marque</th>
<th>etat</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.Articles.Count; i++)
{
<tr>
<td>
#Html.EditorFor(x => x.Articles[i].NumeroSerie, new { htmlAttributes = new { #class = "form-control" } })
</td>
<td>
#Html.EditorFor(x => x.Articles[i].Marque, new { htmlAttributes = new { #class = "form-control" } })
</td>
<td>
#Html.EditorFor(x => x.Articles[i].Etat, new { htmlAttributes = new { #class = "form-control" } })
</td>
</tr>
}
</tbody>
</table>
<input type="hidden" id="hdnLastIndex" value="0" />
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" #*onclick="javascript:window.location.reload()"*#>Annuler</button>
<button type="submit" class="btn btn-primary">Sauvegarder</button>
</div>
</form>
</div>
</div>
</div>
</div>
Everything works fine and the modal popup is displaying with master detail models. But when i click te button to display the modal popup, the background view (Index.cshtml) is changed like the picture below and the CategoryId SelectList isn't populating:
Although this is the index view which is supposed to display in the background of the modal popup:
So why is Index view chaning when displaying the modal popup?
Below is a work demo, you can refer to it.
In controller, make some change in create action.
[HttpGet]
public IActionResult Create()
{
List<Category> categories = _dbcontext.Category.ToList();
ViewBag.ListCategories = new SelectList(categories, "CategoryId", "CategoryName");
Stock stock = new Stock();
stock.Articles.Add(new Article() { ArticleId = 1 });
return PartialView("_AddStockPartialView", stock);
}
2.In the Index view:
remove table (<table>) before <div class="card-body" id="display">
3.In the _Theme.cshtml, check and make sure you already have below code:
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/GestionStock.styles.css" asp-append-version="true" />
...
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
4.result(I use my model data to show the table) :
Say, on a page I have a list of items and a Delete button next to each. Upon clicking, I want to show a pop-up with a confirmation message.
The confirmation dialog and the deletion functionality are put into a view component.
I know I can do like this:
foreach (var item in Model.List)
{
<tr class="row">
<td class="col-12">
#item.Name
<button class="btn btn-danger ml-auto" data-toggle="modal"
data-target="#delete-item-#item.Id">×</button>
<vc:delete-item-dialog id="delete-item-#item.Id" item-id="#item.Id"></vc:delete-item-dialog>
</td>
</tr>
}
But then each delete-item-dialog view component is rendered separately, bloating the size of the generated HTML.
Is it possible to place that view component only in one place, after the end of the list, and provide the item-id parameter more dynamically?
Is it possible to place that view component only in one place, after the end of the list, and provide the item-id parameter more dynamically?
Yeah, you can use ajax to load the view component dynamically. Below is a working demo.
View:
#model List<User>
<table>
#foreach (var item in Model)
{
<tr class="row">
<td class="col-12">
#item.Name
<button class="btn btn-danger ml-auto" onclick="deleteuser(#item.Id)">
×
</button>
</td>
</tr>
}
</table>
<div id="container">
</div>
#section scripts{
<script>
function deleteuser(id){
$.ajax({
type: 'get',
url: 'GetMyViewComponent?id=' + id,
success: function (result) {
$("#container").html(result);
$('.modal').modal('show');
}
})
}
</script>
}
Controller:
public IActionResult UserList()
{
var users = new List<User>
{
new User{ Id = 1, Name = "Mike"},
new User{ Id = 2, Name = "John"},
new User{ Id = 3, Name = "David"},
};
return View(users);
}
public IActionResult GetMyViewComponent(int id)
{
return ViewComponent("PopUp", id);
}
PopUpViewComponent class:
public class PopUpViewComponent : ViewComponent
{
public IViewComponentResult Invoke(int id)
{
return View(id);
}
}
PopUpViewComponent Razor View:
#model int
<div class="modal fade" id="delete-#Model" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure to delete #Model?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Ok</button>
</div>
</div>
</div>
</div>
Result:
I have a problem with passing data from view to controller... When I was clicked button which sending POST - I have got only data for Category.Name. No subcategories, no ids etc. I am thinking I do something wrong with my view but i am not sure...
I need really help. Thanks for everything comments.
That is my View:
#model AplikacjaFryzjer_v2.Models.Category
#{
ViewData["Title"] = "Edytuj kategorię";
Layout = "~/Views/Shared/_Layout.cshtml";
var SubcategoriesData = (IList<AplikacjaFryzjer_v2.Models.Subcategory>)ViewBag.Subcategories;
}
<h1>Edytuj kategorię</h1>
<form method="post">
<div class="form-group row">
<div class="col">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="col-sm-10 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="Name" disabled class="form-control" />
</div>
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<button class="btn btn-primary" type="submit" id="update">Aktualizuj kategorię</button>
</div>
<div class="col">
#if (SubcategoriesData != null)
{
<div class="col-sm-10 col-form-label">
<div id="subcatContainer">
#foreach (var subcategory in SubcategoriesData.ToList())
{
<div class="form-group col-sm-6">
<input asp-for="#subcategory.Name" />
<button class="btn btn-danger" type="button" id="remove">Usuń</button>
<span asp-validation-for="#subcategory.Name" class="text-danger"></span>
</div>
}
</div>
<button type="button" class="btn btn-secondary" id="add">Dodaj podkategorię</button>
</div>
}
else
{
<div id="container" class="col-md-6">
<label id="labelName">Nazwa podkategorii</label>
<input id="inputName" />
<button type="button" class="btn btn-secondary" id="addNew">Dodaj</button>
</div>
}
</div>
</div>
</form>
<script>
$(document).ready(function (e) {
// Variables
var i = #SubcategoriesData.Count()-1;
// Add rows to the form
$("#add").click(function (e) {
var html = '<p /><div class="form-group col-sm-6"><input asp-for="Subcategories[' + i + '].Name" /><button class="btn btn-danger" type="button" id="remove">Usuń</button></div>';
i++;
$("#subcatContainer").append(html).before();
});
// Remove rows from the form
$("#subcatContainer").on('click', '#remove', function (e) {
i--;
$(this).parent('div').remove();
});
// Populate values from the first row
});
</script>
My Actions EditCategory in controller:
[HttpGet]
public ViewResult EditCategory(int Id)
{
var category = _categoriesRepository.GetCategory(Id);
if (category == null)
{
ViewBag.ErrorMessage = $"Kategoria o id = {Id} nie została odnaleziona";
return View("NotFound");
}
ViewBag.Subcategories = category.Subcategories;
return View(category);
}
[HttpPost]
public IActionResult EditCategory(Category category)
{
if (!ModelState.IsValid)
{
// store Subcategories data which has been added
ViewBag.Subcategories = category.Subcategories == null ? new List<Subcategory>() { } : category.Subcategories;
return View("EditCategory");
}
_categoriesRepository.UpdateCategory(category);
return RedirectToAction("ManageCategories");
}
And my object (model):
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public List<Subcategory> Subcategories {get;set;}
}
I have got only data for Category.Name. No subcategories, no ids etc.
Based on your Category model class, the property Subcategories is defined as List<Subcategory>, to make form data values that browser user post could be automatically bound to this property, you can modify the field of Subcategories related to <input asp-for="#Subcategories[i].Name" /> as below.
#model AplikacjaFryzjer_v2.Models.Category
#{
ViewData["Title"] = "Edytuj kategorię";
Layout = "~/Views/Shared/_Layout.cshtml";
var Subcategories = (IList<AplikacjaFryzjer_v2.Models.Subcategory>)ViewBag.Subcategories;
}
<form method="post">
<div class="form-group row">
#*code for other fields*#
<div class="col">
#if (Subcategories != null)
{
<div class="col-sm-10 col-form-label">
<div id="subcatContainer">
#for (int i = 0; i < Subcategories.Count; i++)
{
<div class="form-group col-sm-6">
<input asp-for="#Subcategories[i].Name" />
<button class="btn btn-danger" type="button" id="remove">Usuń</button>
<span asp-validation-for="#Subcategories[i].Name" class="text-danger"></span>
</div>
}
</div>
<button type="button" class="btn btn-secondary" id="add">Dodaj podkategorię</button>
</div>
}
else
{
<div id="container" class="col-md-6">
<label id="labelName">Nazwa podkategorii</label>
<input id="inputName" />
<button type="button" class="btn btn-secondary" id="addNew">Dodaj</button>
</div>
}
</div>
</div>
</form>
Update:
why new inputs added by jquery not sending to my controller? I have got "" in my code jquery
As I mentioned in comment, please not use tag helper syntax on js client. You can try to manually set name and value attribute for new generated input field(s)
like below.
$(document).ready(function (e) {
// Variables
var i = #Subcategories.Count();
// Add rows to the form
$("#add").click(function (e) {
//var html = '<p /><div class="form-group col-sm-6"><input asp-for="Subcategories[' + i + '].Name" /><button class="btn btn-danger" type="button" id="remove">Usuń</button></div>';
var html = '<p /><div class="form-group col-sm-6"><input name="Subcategories[' + i + '].Name" value="" /><button class="btn btn-danger" type="button" id="remove">Usuń</button></div>';
i++;
$("#subcatContainer").append(html).before();
});
// Remove rows from the form
$("#subcatContainer").on('click', '#remove', function (e) {
i--;
$(this).parent('div').remove();
});
// Populate values from the first row
});
Test Result
This is my part of code . Here i can add and submit the values. But while editing submit is not working. Can anyone help me on this. What can be the Issue over here.
#using (Html.BeginForm("LenseAdd", "Stock", FormMethod.Post, new { #class = "form-horizontal form-bordered" }))
{
#Html.AntiForgeryToken()
#Html.HiddenFor(m => m.LenseId)
#Html.HiddenFor(m => m.MaterialId)
<div class="form-group">
<label class="control-label col-lg-3 col-sm-3">Lense Code<span class="text-danger">*</span></label>
<div class="col-lg-6 col-sm-8">
<div class="input-group">
#*<input type="text" class="form-control" />*#
#Html.TextBoxFor(m => m.LenseCode, new { #class = "form-control", #placeholder = "Lense Code", #id = "LenseCode" })
<span class="input-group-addon"><span class="fa fa-money"></span></span>
</div>
#Html.ValidationMessageFor(m => m.LenseCode, null, new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-3 col-sm-3">BrandName<span class="text-danger">*</span></label>
<div class="col-lg-6 col-sm-8">
<div class="input-group">
#Html.DropDownListFor(m => m.BrandId, (IEnumerable<SelectListItem>)ViewBag.BrandList, "--select--", new { #class = "form-control", #id = "ddlbranchId" })
<span class="input-group-addon"><span class="fa fa-money"></span></span>
</div>
#*#Html.ValidationMessageFor(m => m.SalaryTo, null, new { #class = "text-danger" })*#
</div>
</div>
<div class="form-group">
#if (Model != null)
{
if (Model.CategoryList != null)
{
<label class="control-label col-lg-3 col-sm-3">Material</label>
foreach (var item in Model.CategoryList)
{
if (item.CategoryName == "MATERIAL")
{
<div class="col-lg-1 col-sm-3">
<div class="c-form__group -radio -inline">
<label class="c-form__control-text radio_heading">
<label>#item.Name</label>
#if (Model.MaterialId == item.ID)
{
#Html.RadioButtonFor(m => m.MaterialId, true, new { #checked = "checked", #class = "MATERIAL", #id = "MATERIAL_"+item.ID.ToString() })
}
else
{
#Html.RadioButtonFor(m => m.MaterialId, false, new { #class = "MATERIAL", #id = "MATERIAL_" + item.ID.ToString() })
}
<div class="-indicator"></div>
</label>
</div>
</div>
}
}
}
}
</div>
<div class="panel-footer">
<div class="row">
<div class="col-md-10 text-right">
<div class="btn-group">
<button type="submit" class="btn btn-custom fa fa-save -blue" >Save & New</button>
<button type="submit" class="btn btn-custom fa fa-save -skyblue" id="btn_submit">Save</button>
Cancel
</div>
</div>
</div>
</div>
}
I didn't copy the full cshtml page. here if you see any div mismatching please ignore. Any help will be appreciated.
I am trying to add razor to my HTML but now I am getting a Parser Error and do not understand why I get this error:
HTML:
#model ServingTeam.DAL.Members
#{
ViewBag.Title = "New Member";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using (Html.BeginForm("AddMember", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form" }))
{
<div class="row">
<div class="col-md-2">
</div>
<div class="col-sm-6">
<h2>
New Member</h2>
<form class="form-horizontal" role="form" id="memberForm">
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label" />
Name</label>
<div class="col-sm-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control", placeholder = "Name", type = "text", required = "required " })
</div>
</div>
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">
Surname</label>
<div class="col-sm-10">
#Html.TextBoxFor(m => m.Surname, new { #class = "form-control", placeholder = "Surname", type = "text", required = "required " })
</div>
</div>
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">
Cell</label>
<div class="col-sm-10">
#Html.TextBoxFor(m => m.Cell, new { #class = "form-control", placeholder = "Cell", type = "text" })
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">
Email</label>
<div class="col-sm-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control", placeholder = "Enter email address", type = "email", required = "required " })
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-info">
Submit</button>
</div>
</div>
</form>
</div>
<div class="col-md-4">
</div>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
<script>
$("#memberForm").validate();
</script>
This is the error I get:
Encountered end tag "div" with no matching start tag. Are your start/end tags properly balanced? All works fine when I remove Razor.
Any help please?
I did find one error in your markup, don't know if it has anything to do with your current error though.
<form class="form-horizontal" role="form" id="memberForm">
</form>
the using(Html.BeginForm) will actually create a form for you, now you have a form within the form, which is not wizely
<label for="inputName" class="col-sm-2 control-label" />
Name</label>
There you go. the label gets cancelled out twice.