There is no ViewData item of type 'IEnumerableSelectListItem' that has the key country
In your action change ViewBag.countrydrop = item8
to ViewBag.country = item8;
and in View write like this:
@Html.DropDownList("country",
(IEnumerable<SelectListItem>)ViewBag.country,
"Select country")
Actually when you write
@Html.DropDownList("country",
(IEnumerable)ViewBag.country,
"Select country")
or
Html.DropDownList("country","Select Country)
it looks in for IEnumerable<SelectListItem>
in ViewBag
with key country, you can also use this overload in this case:
@Html.DropDownList("country","Select country") // it will look for ViewBag.country and populates dropdown
See Working DEMO Example
There is no ViewData item of type 'IEnumerableSelectListItem' that has the key 'ddlcontent'
From asp-net-mvc-dropdown-list-from-selectlist:
You are missing setting what field is the Text and Value in
the SelectList itself. That is why it does a.ToString()
on each
object in the list. You could think that given it is a list of
SelectListItem
it should be smart enough to detect this... but it is
not.
So in your case, you need to convert List<String>
to List<SelectListItem>
as follow:
ViewBag.ddlcontent = new SelectList(listone.Select(i=> new SelectListItem()
{
Text = i,
Value = i
}).ToList(),"Value" , "Text");
There is no ViewData item of type 'IEnumerableSelectListItem' that has the key 'DiseaseType'
Instead of using a List<string>
and SelectList
in your controller, use a List<SelectListItem>
instead and load that into your ViewBag
:
List<SelectListItem> disease = new List<SelectListItem>();
disease.Add(new SelectListItem { Value = "Select", Text = "Select" });
disease.Add(new SelectListItem { Value = "Cancer", Text = "Cancer" });
disease.Add(new SelectListItem { Value = "Heart", Text = "Heart" });
ViewBag.Diseases = disease;
In your View (Edit.cshtml) use your ViewBag
for the dropdown like so:
@Html.DropDownList("DiseaseType", (IEnumerable<SelectListItem>)ViewBag.Diseases, new { htmlAttributes = new { @class = "form-control" } })
Here I have put "DiseaseType" in instead of your model, but only to demonstrate that when you then post your choice, in order to get that value passed back into your controller, take a string called DiseaseType (use your model instead):
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult TestStack(string DiseaseType)
{
string result = DiseaseType;
return RedirectToAction("Index");
}
After Post Method There is no ViewData item of type 'IEnumerableSelectListItem' that has the key 'DepartmentId'
Just as the error states, the ViewBag
has no such data. Note how you set this value in your [HttpGet]
action:
ViewBag.DepartmentId = dList;
So when you return the view, that value is available to the view. Your [HttpPost]
action returns the same view. But it returns it without any model and, specifically to regarding this error, without ever setting anything in the ViewBag
.
Before you return the view, set the ViewBag
values you need in your controller action. Exactly as you already do in your [HttpGet]
action.
(Since the functionality would be repeated, you are of course encouraged to extract it into a common method rather than just copying/pasting all of the same code. You're also encouraged to use a model instead of relying on ViewBag
, but that may just come down to a matter of personal preference.)
Edit: As requested in a comment below, an example:
For example, notice how you set your ViewBag
value in your [HttpGet]
action:
using (MyConnectionString _context = new MyConnectionString())
{
var list = (from d in _context.Departments
select new
{
d.DepartmentId,
d.DepartmentName
}).ToList();
SelectList dList = new SelectList(list, "DepartmentId", "DepartmentName");
ViewBag.DepartmentId = dList;
}
If you want to set the exact same value in your [HttpPost]
action, then simply do the exact same thing:
if (ModelState.IsValid)
{
// ... the code you have in here is unchanged
}
using (MyConnectionString _context = new MyConnectionString())
{
var list = (from d in _context.Departments
select new
{
d.DepartmentId,
d.DepartmentName
}).ToList();
SelectList dList = new SelectList(list, "DepartmentId", "DepartmentName");
ViewBag.DepartmentId = dList;
}
return View();
System.InvalidOperationException: 'There is no ViewData item of type 'IEnumerableSelectListItem' that has the key 'items'.'
Couple of things:
As mentioned in the comments, change your ViewData["MembershipID"] = items;
to ViewData["items"] = items;
in your Controller
method
Secondly: You need to display the selectlist
item as:
@Html.DropDownList("items",ViewData["items"] as List<SelectListItem>, new { @class="form-control" })
InvalidOperationException: There is no ViewData item of type 'IEnumerableSelectListItem' that has the key 'marka'
fix the view I recommend you to use select instead of dropdown list, it automatically selects an item
<select class="form-control" asp-for="brand" asp-items="@ViewBag.marka"></select>
and fix the get action, by removing Selected and adding to List()
var items = _context.Brand.Select(c => new SelectListItem
{
Value = c.Id,
Text = c.Name
}).ToList();
fix the post action too , remove Bind,
public async Task<IActionResult> Edit(int id, Product product)
and remove from code
string selBra = Request.Form["marka"].ToString();
product.Brand = selBra;
Related Topics
Change Color of Text Within a Winforms Richtextbox
Capturing Process Output via Outputdatareceived Event
Equality Comparison Between Multiple Variables
Looking for C# Equivalent of Scanf
What Is the Correct Way to Cancel an Async Operation That Doesn't Accept a Cancellationtoken
C# Structs: Unassigned Local Variable
Generic Methods and Method Overloading
Linq - What Is the Quickest Way to Find Out Deferred Execution or Not
Why Is Webbrowser_Documentcompleted() Firing Twice
Very Simple Definition of Initializecomponent(); Method
How to Convert a Simple .Net Console Project a into Portable Exe with Mono and Mkbundle
Is This a Covariance Bug in C# 4
How to Programmatically Create, Read, Write an Excel Without Having Office Installed
No Access to the Session Information Through Signalr Hub. Is My Design Is Wrong