There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '****'. [duplicate] - asp.net-mvc-4

This question already has answers here:
The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'
(6 answers)
Closed 4 years ago.
Im trying to do a dropdownlist of Student Enrollments at Student index page.
Student Controller:
public ActionResult Index(string studentEnrollment, string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var students = studentService.GetAll();
//var students = from s in studentService.GetAll()
// select s;
var studentViewModel = Mapper.Map<IEnumerable<Student>, IEnumerable<StudentViewModel>>(students);
if (!String.IsNullOrEmpty(searchString))
{
students = students.Where(s => s.LastName.ToUpper().Contains(searchString.ToUpper())
|| s.FirstMidName.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "name_desc":
students = students.OrderByDescending(s => s.LastName);
break;
case "Date":
students = students.OrderBy(s => s.EnrollmentDate);
break;
case "date_desc":
students = students.OrderByDescending(s => s.EnrollmentDate);
break;
default:
students = students.OrderBy(s => s.LastName);
break;
}
List<Course> courses = new List<Course>();
SelectList studentEnrollment = new SelectList(courses, "CourseID", "Title");
ViewBag.Courses = studentEnrollment;
//if (!string.IsNullOrEmpty(studentEnrollment))
// students = students.Where(m => m.Course.Title == studentEnrollment);
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(students.ToPagedList(pageNumber, pageSize));
}
View:
#using (Html.BeginForm("Index", "Student", FormMethod.Get))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<p>
Find by name: #Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
Enrollment: #Html.DropDownList("Course", (IEnumerable<SelectListItem>)ViewBag.Courses, "Select Course")
<input type="submit" value="Filter" />
</p>
}
error at line:
Enrollment: #Html.DropDownList("Course", (IEnumerable)ViewBag.Courses, "Select Course")
What Im doing wrong?

I have seen a lot of questions where people have had issues with the drop down list and it was because of the view bag. Everything I have seen says you should try to avoid using ViewBag for drop downs. What I would suggest instead is to put your list in your viewmodel.
Public List<SelectListItem> CourseList { get; set; }
I am not seeing in your code where you are populating your course list. On your controller instead of setting the viewbag set this parameter. Something like
foreach(var temp in CourseList){ //Course list being pulled from the database or wherever that data exists
model.CourseList.Add(new SelectListItem{ Text = temp.Name, Value = temp.Value });
}
then on your view your drop down list would be changed to
#Html.DropDownList("Course", Model.CourseList, "Select Course")

Related

PATCH in web API for Many to Many relationships using EF

I'm trying to remove a tag from a list of tags using a PATCH request. But while saving changes to the db context I get the following error:.
This the the PATCH code.
[HttpPatch("{id:int}/{field}", Name = "UpdateNote")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult UpdateNote(int id, string field, JsonPatchDocument<CreateNoteDTO> patchNoteDTO)
{
if (id == 0 || patchNoteDTO == null)
return BadRequest();
var note = _db.Notes.AsNoTracking().FirstOrDefault(u => u.ID == id);
if (note == null)
return NotFound();
CreateNoteDTO createNoteDTO = _mapper.Map<CreateNoteDTO>(note);
if (field.Equals("tags"))
{
var tags = _db.Tags.Where(u => u.Notes.Any(x => x.ID == note.ID)).AsNoTracking().ToList();
List<TagDTO> tagDTOs = new List<TagDTO>();
if (tags.Count > 0)
{
foreach (var tag in tags)
{
TagDTO tagDTO = _mapper.Map<TagDTO>(tag);
tagDTOs.Add(tagDTO);
}
}
createNoteDTO.Tags = tagDTOs;
}
patchNoteDTO.ApplyTo(createNoteDTO, ModelState);
if (!ModelState.IsValid)
return BadRequest(ModelState);
Note updateNote = _mapper.Map<Note>(createNoteDTO);
updateNote.ID = id;
updateNote.ChangedDate = DateTime.Now;
_db.Update(updateNote);
if (!field.ToLower().Equals("category"))
_db.Entry(updateNote).Property(u => u.FKCategory).IsModified = false;
_db.Entry(updateNote).Property(u => u.CreatedDate).IsModified = false;
_db.SaveChanges();
return NoContent();
}
My ApplicationDBContext looks like this:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Note> Notes { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Write Fluent API configurations here
modelBuilder.Entity<Note>()
.HasOne(e => e.Category)
.WithMany(e => e.Notes)
.HasForeignKey(e => e.FKCategory)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Note>()
.HasMany(n => n.Tags)
.WithMany(t => t.Notes)
.UsingEntity(j => j
.ToTable("NoteTag")
.HasData
(
new { NotesID = 1, TagsTagID = 1 },
new { NotesID = 2, TagsTagID = 2 }
));
modelBuilder.Entity<Category>().HasData
(
new Category
{
CategoryID = 1,
CategoryName = "testCategory1",
CreatedDate = DateTime.Now
},
new Category
{
CategoryID = 2,
CategoryName = "testCategory2",
CreatedDate = DateTime.Now
}
);
modelBuilder.Entity<Tag>().HasData
(
new Tag
{
TagID = 1,
TagName = "testTag1",
CreatedDate = DateTime.Now
},
new Tag
{
TagID = 2,
TagName = "testTag2",
CreatedDate = DateTime.Now
}
);
modelBuilder.Entity<Note>().HasData
(
new Note
{
ID = 1,
Title = "testTitle1",
Description = "testDescription1",
FKCategory = 1,
Deleted = false,
CreatedDate = DateTime.Now,
ChangedDate = DateTime.Now,
},
new Note
{
ID = 2,
Title = "testTitle2",
Description = "testDescription2",
FKCategory = 2,
Deleted = false,
CreatedDate = DateTime.Now,
ChangedDate = DateTime.Now,
}
);
}
}
Any suggestions?
Thanks in advance.
Initially I tried without the following if condition:
CreateNoteDTO createNoteDTO = _mapper.Map<CreateNoteDTO>(note);
if (field.Equals("tags"))
{
var tags = _db.Tags.Where(u => u.Notes.Any(x => x.ID == note.ID)).AsNoTracking().ToList();
List<TagDTO> tagDTOs = new List<TagDTO>();
if (tags.Count > 0)
{
foreach (var tag in tags)
{
TagDTO tagDTO = _mapper.Map<TagDTO>(tag);
tagDTOs.Add(tagDTO);
}
}
createNoteDTO.Tags = tagDTOs;
}
But I got a different error:
I wanted to remove a tag form the following object using a PATCH request:
Note Object

Retrieve selected checkboxes in an array into the controller

How can i retrieve the selected checkbox in the controller.
This is the main view where the user can choose a request access.
#using (Html.BeginForm("addBatch_CARF", "CARF", FormMethod.Post, new { #name = "register" }))
{
#Html.ValidationSummary(true)
<div id="formAlert" class="alert alert-danger">
<a class="close">×</a>
<strong>Warning!</strong> Make sure all fields are filled and try again.
</div>
var catName = "";
var displayCan = "";
var candidates = "";
for (int i = 0; i < Model.Count; i++)
{
if (catName != Model[i].request_category)
{
<li class="list-group-item list-group-item-success">
#Html.DisplayFor(modelItem => Model[i].request_category)
<span class="pull-right" style="margin-right:60px;">Special Instructions</span>
</li>
catName = Model[i].request_category;
displayCan = catName;
}
if (displayCan == Model[i].request_category)
{
candidates = Model[i].request_name;
<div class="checkbox_request">
#Html.CheckBoxFor(model => model[i].isSelected, new { #class = "is_selected" })
#Html.DisplayFor(model => model[i].request_name)
#if(Model[i].request_name == "Folder Access")
{
<span class="label label-danger">Pls specify all the drive path. Note: For accessing of drives outside PETC please proceed to Online CARF</span>
}
<span class="pull-right">
#Html.EditorFor(model => model[i].special_instruction)
</span>
#Html.HiddenFor(model => model[i].request_type_id)
#Html.HiddenFor(model => model[i].system_roles_id)
</div>
}
}
<li class="list-group-item list-group-item-success">
Access to:
</li>
<div id="employeeAdd">
#{Html.RenderAction("AddRequestor"); }
</div>
<p class="request_btn">
<button type="submit" class="btn btn-primary" id="addbtn">Save</button>
</p>
}
I have only rendered this view AddRequestor in selecting or adding an employee.
<table class="table table-hover">
#for (int i = 0; i < Model.Count; i++){
<tr>
<th>
#Html.CheckBox("checkbox", new { #class = "is_selected" })
#Html.DisplayFor(model => model[i].FullName)
#Html.HiddenFor(model => model[i].Emp_Badge_No)
</th>
</tr>
}
</table>
The main goal of this is all the chosen employees must have also all the chosen request access.
[HttpPost]
public ActionResult addBatch_CARF(List<Request_Type> list, List<Employees_All_vw> emp, string[] checkboxes)
{
foreach (var x in emp)
{
int num = 1;
bool z = Convert.ToBoolean(num);
if (x.checkbox == z)
{
//add data into CARF table
CARF carf = new CARF();
carf.requestor = x.Emp_Badge_No;
carf.carf_type = "BATCH CARF";
carf.created_by = #User.Identity.Name.Remove(0, 9).ToLower();
carf.created_date = System.DateTime.Now;
carf.active_flag = true;
db.CARves.Add(carf);
db.SaveChanges();
int id = carf.carf_id;
//add data into Request Access Table
foreach (var i in list)
{
int val = 1;
bool y = Convert.ToBoolean(val);
if (i.isSelected == y)
{
Request_Access ra = new Request_Access();
ra.request_access_id = 1;
ra.carf_id = id;
ra.request_type_id = i.request_type_id;
ra.special_instruction = i.special_instruction;
ra.ra_assignee = i.system_roles_id;
ra.dept_approval = null;
ra.dept_approval_date = null;
ra.dept_remarks = null;
ra.final_approval = null;
ra.final_approval_date = null;
ra.final_remarks = null;
ra.acknowledge_by = null;
ra.acknowledge_date = null;
ra.journal = null;
ra.closed_by = null;
ra.closed_date = null;
ra.verified_by = null;
ra.verified_date = null;
db.Request_Access.Add(ra);
}
}
db.SaveChanges();
}
TempData["MessageAlert"] = "Successfully created!";
return RedirectToAction("Batch_CARF");
}
}
I've got an error on this line if (x.checkbox == z)
Operator '==' cannot be applied to operands of type 'string[]' and 'bool'
Your parameter string[] checkboxes contains values that are typeof string (either "True" or "False") so you would need to use the Convert.ToBoolean() method before comparing if (x.checkbox == z). However this will not work since #Html.CheckBox("checkbox", ..) generates 2 inputs type="checkbox" with value="True" and a type="hidden" with value="False" so if its checked, both true and false post back and if its unchecked, then only false is posted. There is no way you can possibly match up which values belong to which employee.
Instead create a view model to represent the selection of employees
public class EmployeeVM
{
public string BadgeNumber { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
And then in your AddRequestor() method (assumes you have an Employees table)
public ActionResult AddRequestor()
{
List<EmployeeVM> model = db.Employees.Where(e => e.active_flag).Select(e => new EmployeeVM
{
BadgeNumber = e.Emp_Badge_No,
Name = e.FullName
}.ToList();
return PartialView(model);
}
and in the view
#model List<EmployeeVM>
#for (int i = 0; i < Model.Count; i++)
{
#Html.HiddenFor(m => m[i].BadgeNumber)
<label>
#Html.CheckboxFor(m => m[i].IsSelected)
<span>#Html.DisplayFor(m => m[i].Name)</span>
</label>
}
And finally, in the POST method
[HttpPost]
public ActionResult addBatch_CARF(List<Request_Type> list, List<EmployeesVM> employees)
{
// To get the selected employees
IEnumerable<string> selectedEmployees = employees.Where(e => e.IsSelected);
foreach(EmployeesVM employee in selectedEmployees)
{
....
carf.requestor = employee.BadgeNumber;
....

fill #html.dropdownlist from other #html.dropdownlist by same table using ajax

I'm working on some search/filter functionality by using dropdownlists
the question is how can i populate the second dropdownlist by choosing item in first ddl
if all data saved in ont table.
i have table of CarType that has manufacturer, model and year.
when i choose a manufacturer in first ddl the second will filled by models of this manufacturer.
Code i have in Controller:
public ActionResult Search(string manufacturer, string carModel, string searchString, string gear)
{
using (CarTypesLogic logic = new CarTypesLogic())
{
IEnumerable<CarType> allCarTypes = logic.GetAllCarTypes();
var manufacturerList = new List<string>();
var modelList = new List<string>();
var gearList = new List<string>();
var foundManufacturer = from maker in allCarTypes
orderby maker.Manufacturer
select maker.Manufacturer;
var foundModels = from model in allCarTypes
orderby model.Model
select model.Model;
var foundGears = from transmission in allCarTypes
select transmission.Gear;
manufacturerList.AddRange(foundManufacturer.Distinct());
modelList.AddRange(foundModels.Distinct());
gearList.AddRange(foundGears.Distinct());
ViewBag.Manufacturer = new SelectList(manufacturerList);
ViewBag.Gear = new SelectList(gearList);
ViewBag.carModel = new SelectList(modelList);
var foundCars = from car in allCarTypes select car;
if (!String.IsNullOrEmpty(manufacturer))
{
foundCars = foundCars.Where(car => car.Manufacturer == manufacturer);
}
if (!String.IsNullOrEmpty(gear))
{
foundCars = foundCars.Where(car => car.Gear == gear);
}
if (!String.IsNullOrEmpty(searchString))
{
foundCars = foundCars.Where(car => car.Manufacturer.ToLower().Contains(searchString.ToLower()));
}
if (!String.IsNullOrEmpty(carModel))
{
foundCars = foundCars.Where(car => car.Model == carModel);
}
return View(foundCars);
}
}
My cshtml code:
#using (Html.BeginForm())
{
<p>
Manufacturer: #Html.DropDownList("manufacturer", String.Empty)
Model: #Html.DropDownList("carModel",String.Empty)
Gear: #Html.DropDownList("gear", String.Empty)
Free Type: #Html.TextBox("SearchString")
<br />
<input type="submit" value="Search"/>
</p>
}
you need to use Cascaded DropDownList.
here are few examples:
http://mvc4beginner.com/Sample-Code/Cascading-DropDownList/APS-.NET-MVC-Cascading-DropdownLists-Using-LINQ.html
http://code.msdn.microsoft.com/Cascading-DropDownList-in-833683f9/sourcecode?fileId=48387&pathId=451527572
http://www.itorian.com/2013/02/cascading-dropdownlist-in-aspnet-mvc.html

Binding The Collection of data in drop down list in mvc4

I am having Code like below in my view.
#if (!Model.DisableBuyButton || !Model.DisableWishlistButton)
{
<div class="qutblk">
<span>#Html.LabelFor(model => model.EnteredQuantity, new { #class = "input-small" }, " :")</span>
#if (Model.AllowedQuantities.Count > 0)
{
#Html.DropDownListFor(model => model.EnteredQuantity, Model.AllowedQuantities, new { #class = "input-small" })
}
else
{
#Html.TextBoxFor(model => model.EnteredQuantity, new { #class = "input-small" })
}
} #Html.Widget("productdetails_add_info")
</div>
Here if i have the ALLOWEDQUANTITIES.COUNT is more then 0.I need drop down list for quantities.
For example if the quantity is 5 i need 1,2,3,4,5 numbers in my drop down list.And if the quantity is 7 i need 1,2,3,4,5,6,7 numbers in my drop down.
But my problem is i am not able to bind the data like numbers in my drop down that means if the quantity is 5 it simply displaying only 5 number in drop down list not displaying 1,2,3,4,5.
You may try something like this:
public static class MyLists
{
public static List<SelectListItem> GetList()
{
List<SelectListItem> result = new List<SelectListItem>(int AllowedQuantities)
for(int i = 0; i < AllowedQuantities; i++)
{
var number = i + 1;
var item = new SelectListItem {text = number.ToString(), value = number.ToString()};
result.Add(item);
}
return result
}
}
In your view:
#Html.DropDownListFor(model => model.EnteredQuantity, MyLists.GetList(model.EnteredQuantity), new { #class = "input-small" })

Why does this controller double the inserts when I try to archive the results of the Bing Search API?

I'm trying to archive my search results for a term by
Using the Bing API in an async controller
Inserting them into database using Entity Framework
using the Bing API and insert them into a database using entity framework. For whatever reason it is returning 50 results, but then it enters 100 results into the database.
My Controller Code:
public class DHWebServicesController : AsyncController
{
//
// GET: /WebService/
private DHContext context = new DHContext();
[HttpPost]
public void RunReportSetAsync(int id)
{
int iTotalCount = 1;
AsyncManager.OutstandingOperations.Increment(iTotalCount);
if (!context.DHSearchResults.Any(xx => xx.CityMarketComboRunID == id))
{
string strBingSearchUri = #ConfigurationManager.AppSettings["BingSearchURI"];
string strBingAccountKey = #ConfigurationManager.AppSettings["BingAccountKey"];
string strBingUserAccountKey = #ConfigurationManager.AppSettings["BingUserAccountKey"];
CityMarketComboRun cityMarketComboRun = context.CityMarketComboRuns.Include(xx => xx.CityMarketCombo).Include(xx => xx.CityMarketCombo.City).First(xx => xx.CityMarketComboRunID == id);
var bingContainer = new Bing.BingSearchContainer(new Uri(strBingSearchUri));
bingContainer.Credentials = new NetworkCredential(strBingUserAccountKey, strBingAccountKey);
// now we can build the query
Keyword keyword = context.Keywords.First();
var bingWebQuery = bingContainer.Web(keyword.Name, "en-US", "Moderate", cityMarketComboRun.CityMarketCombo.City.Latitude, cityMarketComboRun.CityMarketCombo.City.Longitude, null, null, null);
var bingWebResults = bingWebQuery.Execute();
context.Configuration.AutoDetectChangesEnabled = false;
int i = 1;
DHSearchResult dhSearchResult = new DHSearchResult();
List<DHSearchResult> lst = new List<DHSearchResult>();
var webResults = bingWebResults.ToList();
foreach (var result in webResults)
{
dhSearchResult = new DHSearchResult();
dhSearchResult.BingID = result.ID;
dhSearchResult.CityMarketComboRunID = id;
dhSearchResult.Description = result.Description;
dhSearchResult.DisplayUrl = result.DisplayUrl;
dhSearchResult.KeywordID = keyword.KeywordID;
dhSearchResult.Created = DateTime.Now;
dhSearchResult.Modified = DateTime.Now;
dhSearchResult.Title = result.Title;
dhSearchResult.Url = result.Url;
dhSearchResult.Ordinal = i;
lst.Add(dhSearchResult);
i++;
}
foreach (DHSearchResult c in lst)
{
context.DHSearchResults.Add(c);
context.SaveChanges();
}
AsyncManager.Parameters["message"] = "The total number of results was "+lst.Count+". And there are " + context.DHSearchResults.Count().ToString();
}
else
{
AsyncManager.Parameters["message"] = "You have already run this report";
}
AsyncManager.OutstandingOperations.Decrement(iTotalCount);
}
public string RunReportSetCompleted(string message)
{
string str = message;
return str;
}
}
Here is how I am calling it from my asp.net mvc 4 page.
#Ajax.ActionLink("Run Report", "GatherKeywordsFromBing", "DHWebServices",
new { id=item.CityMarketComboRunID},
new AjaxOptions { OnSuccess = "ShowNotifier();", UpdateTargetId = "TopNotifierMessage", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, LoadingElementId = strCityMarketComboProgressID, LoadingElementDuration = 1000 },
new { #class = "ViewLink" })
<span class="ProgressIndicator" id="#strCityMarketComboProgressID"><img src="#Url.Content("~/Content/img/SmallBall.gif")" alt="loading" /></span>
For whatever reason all of
Try saving only once:
foreach (DHSearchResult c in lst)
{
context.DHSearchResults.Add(c);
}
context.SaveChanges();
Also there's nothing asynchronous in your code, so there's no point of using asynchronous controller. Not only that it won't improve anything but it might make things worse.