DropDown Menu using IEnumerable SelectList - c#

I am getting the following error when trying to select an item from my drop down list and submit this.
An exception of type 'System.InvalidOperationException' occurred in
System.Web.Mvc.dll but was not handled in user code Additional
information: There is no ViewData item of type
'IEnumerable' that has the key 'RoleName'.
If anyone can help me figure out how to fix this error I would really appreciate it as I haven't been able to fix it yet and been stuck for quite a long time and google hasn't provided a solution yet!
This is my controller code
[AllowAnonymous]
public ActionResult Index()
{
var roles = context.Roles.ToList();
return View(roles);
}
[Authorize(Roles = "canEdit")]
public ActionResult ManageUserRoles()
{
var list = context.
Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
ViewBag.Roles = list;
return View();
}
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
ApplicationUser user = context.Users.FirstOrDefault(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase));
if (user != null)
{
UserManager.AddToRole(user.Id, RoleName);
}
return View("ManageUserRoles");
}
This is my ManageUserRoles View
#{
ViewBag.Title = "ManageUserRoles";
}
<h2>Manage User Roles</h2>
#Html.ActionLink("Create New Role", "Create") | #Html.ActionLink("View User Roles", "Index")
<hr />
<h2>Role Add to User</h2>
#using (Html.BeginForm("RoleAddToUser", "Roles"))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<p>
User Name : #Html.TextBox("UserName")
Role Name: #Html.DropDownList("RoleName", (IEnumerable<SelectListItem>) ViewBag.Roles, "Select ...")
</p>
<input type="submit" value="Save" />
}
<hr />

Problem is (I assume) that you are visiting View("ManageUserRoles") from your ActionResult RoleAddToUser and not from ManageUserRoles. So ViewBag does not exists.
return View("ManageUserRoles");
starts to render view. However on the view you have dropdown which wants ViewBag.Roles ... but ... where do you set data into it? This exception is thrown because ViewBag.Roles just not exist.
If you want run ActionResult ManageUserRoles before returning the view, you have to call redirect.
return RedirectToAction("ManageUserRoles");
If you want render view without execute code in that method, you have to create ViewBag.Roles in RoleAddToUser. Please, note that ViewBag data are deleted after every request.

Verify that your model (whether its a domain model or some other viewmodel) has a field of string type called "RoleName", this needs to match the first argument of your DropDownList call so the page knows what variable to post the data back in on form submit.

Related

Trying to add new data to table leads to an error: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type

I am taking an ASP.NET course an Udemy. Unfortunately, it's an old course, and I don't believe to get an answer there.
Now, what exactly is going on.
At this stage of the course, I need to work with Customers. The part that should show the list of customers, or the details of a specific customer, are working fine.
However, when I am trying to add a new customer to the database, the app crashes.
The full quote of the error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult CustomerDetails(Int32)' in 'VidlyExercise1.Controllers.CustomersController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
I've trying to copy the exact code from lessons, but still something doesn't match.
The Customers Controller code:
public class CustomersController : Controller
{
private ApplicationDbContext _context;
public CustomersController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool Disposing)
{
_context.Dispose();
}
// GET: Customers
[Route("customers")]
public ActionResult CustomersList()
{
var customers = _context.Customers.Include(c => c.MembershipType).ToList();
var viewModel = new CustomersIndexViewModel
{
Customers = customers
};
return View(viewModel);
}
[Route("customers/{id}")]
public ActionResult CustomerDetails(int id)
{
var customer = _context.Customers.Include(m => m.MembershipType)
.SingleOrDefault(c => c.Id == id); //Eager loading
var viewModel = new CustomerDetailsViewModel
{
Name = customer.Name,
MembershipType = customer.MembershipType,
Birthdate = customer.Birthdate
};
return View(viewModel);
}
[Route("customers/new")]
public ActionResult New()
{
var membershipTypes = _context.MembershipTypes.ToList();
var viewModel = new NewCustomerViewModel()
{
MembershipTypes = membershipTypes,
Customer = new Customer()
};
return View("New", viewModel);
}
[HttpPost]
public ActionResult Create(Customer customer)
{
_context.Customers.Add(customer);
_context.SaveChanges();
return RedirectToAction("CustomersList", "Customers");
}
Now, when I click the button just to enter the View for adding a new Customer, it opens up fine.
But when I try to click the "Save" button, I get an error I posted above.
I even tried, changing the code in "Create" method, even just to post 404.
return HttpNotFound();
So, as I understand the Create method doesn't even get to the part of doing anything, it just crashes.
Here's the View code:
#model VidlyExercise1.ViewModels.NewCustomerViewModel
#{
ViewBag.Title = "New";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>New Customer</h2>
#using (Html.BeginForm("Create", "Customers", FormMethod.Get))
{
<div class="form-group">
#Html.LabelFor(m => m.Customer.Name)
#Html.TextBoxFor(m => m.Customer.Name, new { #class = "form-control" })
</div>
<div class="form-group">
#Html.LabelFor(m => m.Customer.Birthdate)
#Html.TextBoxFor(m => m.Customer.Birthdate, new { #class = "form-control" })
</div>
<div class="form-group">
<label>
#Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsLetter) Subscribed to newsletter
</label>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Customer.MembershipTypeId)
#Html.DropDownListFor(m => m.Customer.MembershipTypeId,
new SelectList(Model.MembershipTypes, "Id", "Name"),
"Select Membership Types", new { #class = "form-control" })
</div>
#Html.HiddenFor(m=>m.Customer.Id)
<button type="submit" class="btn btn-primary">Save</button>
}
I think the example code from mentor on GitHub is almost identical. The View html is different, because it also includes further lessons, but the Controller code seems to be correct.
I know the there is a need to post something I already tried. I did google for potential fixes, and did try some of them, but the problem is, I don't know what I am looking for.
Some examples of what I found in similar questions, but which didn't help:
#using (Html.BeginForm("Search", "Person",FormMethod.Get))
Don't use a variable "Id" in ActionResult CustomerList: I never used one
Maybe something else I don't remember now.
One more thing: When I click the button, the path shows: "localhost\Customers\Create"
There is no View suited for this, and I am not sure that it's what supposed to happen.
Can you please help to find what's wrong? Again, it's hard to find an error myself, since I only recently started learning ASP.NET (with the said course) and new to it.
And one more question: The "create" button should get a "Customer" from somewhere, but where in the View code I actually "send" it?
Thank you in advance,
Evgenie
I apologize for trouble, I seems I found what was the problem.
Upon clicking "Save" button, the route was trying to redirect to "customer/save".
And there was a line:
public ActionResult CustomerDetails(int)
So, the program was trying to open route "customer/save" as if the word "save" was an Id!
So, instead of even getting to the code in the ActionResult Save, it tried to find a customer with non-existing Id="Save".
To fix that, all I needed is to change the line declaring the ActionResult CustomerDetails, that it will only accept integers as an Id's.
Like this:
[Route("customers/{id:int}")]
public ActionResult CustomerDetails(int id)
I didn't have to do any changes to "Save" method in CustomersController, or to the relevant View.

Resending a Model resets values in asp.net mvc?

This is my View:
#model test2.Models.ChatModel
#{
ViewBag.Title = "Channel";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<center>
<h2>Channel: #Model.channelName</h2>
#{
foreach (string line in Model.chatLog) {
<div>#line</div>
}
}
<br />
#using (Html.BeginForm("sendMessage", "Home", FormMethod.Post)) {
#Html.TextBoxFor(model => model.message)
<button type="submit"> Send Message </button>
}
</center>
Here is my Controller:
public ActionResult sendMessage(ChatModel model) {
//send message somewhere
//this is not working
return RedirectToAction("Channel", "Home", new { channel = model.channelName });
//this is working
return RedirectToAction("Channel", "Home", new { channel = "test" });
}
The error happens in the redirectToAction method. Somehow "model.channelName" is empty, but #Model.channelName in my view is correctly displaying the channel name.
It looks like when you send a Model to a view, and "resend" this model back to a controller, the informations are lost.
Is there an easy way to solve this?
PS Step by step:
Model gets channelName
Model is send to view
View correctly displays data from model
adding message to Model
sending model to controller
model does NOT contain information from step 1
You need to include model.channelName in the form. Try adding a:
#Html.HiddenFor(model => model.channelName)
Anything not posted by the form, will be null in your model (including your chatlog)
Actually the values model properties should be rendered as input elements within the form that is posted back to controller action. The properties which are not included would loose their values.
What you can do is create a hidden field for those to post :
#using (Html.BeginForm("sendMessage", "Home", FormMethod.Post)) {
#Html.TextBoxFor(model => model.message)
#Html.HiddenFor(model => model.channelName)
<button type="submit"> Send Message </button>
}
You would need to add same way other properties too that are posting null at action and you need those for some processing.
Hope it helps.

how to catch id in aspx page in MVC

I'm working on a MVC production project.
In my Production details view I have some buttons to get some more data from the database, but for this I need the id of the Product. I can see it exist but can I catch it?
Here's my controller that return data:
public ActionResult Details(long AProductionOrderId)
{
ProductionOrderList item = new ProductionOrderList();
item = ProductionOrderReg.GetProductionOrders(conn, AProductionOrderId);
ViewData["item"] = item;
return View();
}
Here's my details page when it load, I can see the id, but how to catch and use it in the buttons in the left to bring more date ?
You could use a hidden input on your view page to submit the ID.
your View:
<form method="post">
<button type="submit">Button Text</button>
<input type="hidden" name="AProductionOrderId" value="#ViewData['item']">
</form>
i wrote this im my controller
ViewData["id"] = AProductionOrderId;
and catched it in my view
long id = Convert.ToInt64( ViewData["id"]);
If you controller is:
public ActionResult Details(long AProductionOrderId)
{
var item = ProductionOrderReg.GetProductionOrders(conn, AProductionOrderId);
ViewBag.ProductionOrderId = AProductionOrderId;
return View(item);
}
then your AProductionOrderId will be in the ViewBag although I don't see the reason why you need it since whatever the type of item is (single object instance or list of objects) it contains your ID as a property because you're fetching the item by this ID. Anyway in your model you then need to declare your model like this:
#model YourModelNamespace.ProductionOrderList
and now you can access any property of your model in your view. But if you really want you can access it via ViewBag like this:
#{
long AProductionOrderId = Viewbag.AProductionOrderId;
}

Model object incomplete when try to update

I have the following action methods:
public ActionResult ProfileSettings()
{
Context con = new Context();
ProfileSettingsViewModel model = new ProfileSettingsViewModel();
model.Cities = con.Cities.ToList();
model.Countries = con.Countries.ToList();
model.UserProfile = con.Users.Find(Membership.GetUser().ProviderUserKey);
return View(model); // Here model is full with all needed data
}
[HttpPost]
public ActionResult ProfileSettings(ProfileSettingsViewModel model)
{
// Passed model is not good
Context con = new Context();
con.Entry(model.UserProfile).State = EntityState.Modified;
con.SaveChanges();
return RedirectToAction("Index", "Home");
}
#using (Html.BeginForm("ProfileSettings", "User", FormMethod.Post, new { id = "submitProfile" }))
{
<li>
<label>
First Name</label>
#Html.TextBoxFor(a => a.UserProfile.FirstName)
</li>
<li>
<label>
Last Name</label>
#Html.TextBoxFor(a => a.UserProfile.LastName)
</li>
...
<input type="submit" value="Save" />
...
When I hit submit received model in POST method is incomplete. It contains FirstName, LastName etc. But UserID is null. So I can't update object. What am I doing wrong here?
MVC reconstructs your model only based on what's coming in the request. In your particular case, you are only submitting the FirstName and the LastName, because those are the only #Html.TextBoxFor() calls included in your View. MVC models don't behave like ViewState, it isn't stored anywhere.
You also don't want to include your entire Entity in your view-model. If all you need is the ID then that should be all you include. Then you'd load your entity again from your DAL, update the properties that need to be altered, and then save your changes.
You should store the UserId as a hidden field in the form.
Add a html tag HiddenFor in your view, and make sure that you are populating UserId in your Get action :
#using (Html.BeginForm("ProfileSettings", "User", FormMethod.Post, new { id = "submitProfile" }))
{
#Html.HiddenFor(a => a.UserProfile.UserId)
// your code here..
}

The ViewData item that has the key 'IDMateria' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

I have the following code in my Simulacion controller:
[Authorize]
public ActionResult Create()
{
Simulacion simulacion = new Simulacion();
MateriaRepository materia = new MateriaRepository();
EvaluadorRepository evaluador = new EvaluadorRepository();
ViewData["Materias"] = new SelectList(materia.FindAllMaterias().ToList(), "ID", "Nombre");
ViewData["Evaluadors"] = new SelectList(evaluador.FindAllEvaluadors().ToList(), "ID", "Nombre");
return View(simulacion);
}
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Create(Simulacion simulacion)
{
if (ModelState.IsValid)
{
repo.Add(simulacion);
repo.Save();
return RedirectToAction("Details", new { id = simulacion.ID });
}
return View(simulacion);
}
When I run the Create Action I can see the dropdownlist working just fine. I can select from a list of existing Materias or Evaluators. When I try to the POST Create Action, I receive the exception posted up top.
Here's how Idisplay the dropdownlist:
<div class="editor-field">
<%: Html.DropDownList("IDMateria", (SelectList)ViewData["Materias"])%>
<%: Html.ValidationMessageFor(model => model.IDMateria) %>
</div>
I'm stumped because I've used this same code in another area of my same application and it works, I just changed the variable names to fit this use case.
It looks like you need to reset the ViewData if the modelstate isn't valid, as it won't get persisted automatically.

Categories

Resources