assumed that i have Roles table like this :
tb_role
RoleId Role_Name
1 SalesCreate
2 SalesEdit
3 AgentCreate
4 AgentEdit
i want to list role for Sales in checkbox (SalesCreate and SalesEdit, so its only have 2 checboxes). I made my tb_role using aspnet configuration, so it doesn't use entities.
here my Controller:
RegisterModel account = new RegisterModel();
account.Roles = new MultiSelectList(Roles.GetAllRoles());
and my View:
<td><select id="Roles" name="Roles">
<option>Sales</option>
<option>Agent</option>
</select>
</td>
#foreach (var item in Model.Roles)
{
<label for="#item.Value">
<input type="checkbox" id="#item.Value" name="RolesSelected" value="#item.Value" #(item.Selected ? "checked" : "") />#item.Text</label>
}
when i run my project, my checkbox list all of the roles in tb_role. I want that if I choose Sales, my checkbox list all the Roles for Sales (SalesCreate and SalesEdit). how to do that ?
thanks a lot
Couple of ways to do this. One way is this:
Surround the <select> with a <form> tag and do a submit on change.
in your controller:
public ActionResult Index(..., string role)
{
//... rest of your code
RegisterModel account = new RegisterModel();
account.Roles = new MultiSelectList(Roles.GetAllRoles().Where(w => w.StartsWith(role));
//... rest of your code
}
Related
i want to filter products on my site. but when i click on my filter-button i get the error message that the ressource cannot be found.
Description: HTTP 404. The resource or any of its dependencies may have been removed, renamed, or temporarily unavailable. Check the URL below and make sure it's spelled correctly.
Requested URL: /Views/Product/Index.cshtml
here's my filter (simplified):
<form action="~/Views/Product/Index.cshtml" method="get">
<div class="row">
<div class="row">
<select class="event-type-select" name="category">
<optgroup label="optgroup1">
<option> option1</option>
<option> option2</option>
</optgroup>
<optgroup label="optgroup2">
<option> option3</option>
</optgroup>
</select>
</div>
<div class="row">
<button type="submit" id="button" class="btn btn-default">filter!!!</button>
</div>
</div>
</form>
i didn't add any code to my controller. i dont know if that is the problem, but i also dont know what i would have to add in the controller then.
hope anyone can help me to solve this.
total beginner.
This is linking to a view:
"~/Views/Product/Index.cshtml"
You don't want to link to a page, you want to link to an action on the controller. For example, if you have a controller action such as:
public class ProductsController : Controller
{
public ActionResult Create(Product product)
{
// code to create a "product"
}
}
Then, at least using default routing conventions (unless you modify them), the URL would be something like:
"/Products/Create"
(Note: In the above code I'm using "Products" instead of "Product". It may be a slight nitpick, but semantically it usually works better to pluralize collections. And the RESTful URLs for these actions are on what is semantically a collection.)
In MVC you're not creating "pages" that interact with each other. You're creating "controllers" which get invoked by a request, perform logic using "models", and direct control to a "view" to render the result back to the user.
The ASP.NET MVC Framework has a variety of tooling available to help simplify much of this as well, such as providing "HTML Helpers" which will determine the URL for you based on the routing rules and the controller/action specified. You'd definitely do well to walk through some tutorials on ASP.NET MVC and continue to get more of a feel for how to use the framework.
The problem is the server could not find what was requested.
You need to add this to the controller:
public class ProductController : Controller
{
public ActionResult Index()
{
return View();
}
}
You need to add the Index.cshtml to the product folder under the folder Views
If you are going to pass parameters to the page in order to apply a filter:
public class ProductController : Controller
{
public ActionResult Index()
{
// Populate products or from a database
List<Product> products = new List<Product>();
Product prod = new Product();
prod.Name = "My Product";
products.Add(prod);
return View(products);
}
}
The model would look like this under the folder Models:
public class Product
{
public string Name { get; set; }
}
Then the Index.cshtml would have a model like so:
#model List<Product>
#foreach (var product in Model)
{
<p> #product.Name </p>
}
For example purposes only this is a form that you would want to create if you only wanted to show selected Products from a dropdown:
#model List<Product>
#using (#Html.BeginForm("FilterProduct", "Product", new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<select id="filter" name="filter" multiple>
<option value="">Products</option>
#foreach (var product in Model)
{
<option value="#product.Name">#product.Name</option>
}
</select>
<button type="submit" id="button" class="btn btn-default">filter!!!
</button>
}
So now your controller would look like this:
public class ProductController : Controller
{
public ActionResult Index()
{
// Populate products or from a database
List<Product> products = new List<Product>();
Product prod = new Product();
prod.Name = "My Product";
products.Add(prod);
return View(products);
}
public ActionResult FilterProduct(string[] filter)
{
// Filter your list of products based on the parameter
return View("Index", products);
}
}
I have a .NET Core 2 MVC project, and I am very new to this. I have a Details View that contains data for a selected user. I am using a ViewModel to display that selected user's data and it is all working fine. What I am trying to ultimately do is allow for the cloning of one user's Identity Roles to the current selected user. So I have in my View a panel that contains a SelectList to where you can select some other user (I'm calling them the source user) to get their currently assigned Identity Roles. I then want to have a button that says "Clone" and on click remove all Roles for the current user and then add them to the Roles that the selected source user is assigned to.
Here is what I am using for my drop-down list:
View:
<select asp-for="EmployeeList" class="form-control"
asp-items="#Model.EmployeeList"
onchange="this.form.submit()">
<option disabled selected>-</option>
</select>
Controller:
var employeeList = _employeeRepository.GetEmployeeList().Select(e => new
{
Id = e.EmployeeId,
Value = e.PreferredFirstName != null ? e.LastName + ", " + e.PreferredFirstName : e.LastName + ", " + e.FirstName
});
ViewModel:
public SelectList EmployeeList { get; set; }
In my View I have a form for the post:
#using (Html.BeginForm("Details", "ApplicationUser", FormMethod.Post))
So I need to pass the selected id (string) of my source user drop-down list to the [HttpPost] Details action in my controller. I also want to maintain the selected option in my drop-down list in the View. I also want to maintain all of the data that is being displayed in my ViewModel.
Your help is greatly appreciated.
As said before, your view is really not making sense. However we can fix that.
Viewmodel:
public List<SelectListItem> EmployeeList { get; set; }
View:
<select id="employee-select" asp-for="EmployeeId" class="form-control" onchange="postEmployee()" asp-items="#Model.EmployeeList">
<option disabled selected>-</option>
</select>
<script>
function postEmployee() {
var id = $('#employee-select').val();
if (id !== '' && id != null) {
var url = '../CONTROLLERNAME/UpdateUser/' + id;
var settings = {
"url": url,
"method": 'POST'
};
$.ajax(settings);
}
}
</script>
Controller:
[HttpPost("UpdateUser/{id}")]
public IActionResult UpdateUser([FromRoute] int id)
{
// Do things with id
}
I have department table with DeptId, DeptName and IsSelected Columns.
I have two records and for one of the record is selected is false and for another record isselected is true. So now when Isselected is true, value should be checked for respective record when page loads for firsttime in MVC5. I have below code, how can I modify that to achieve this requirement.
#model RadioButtonForApp_MVC.Models.Employee
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using (Html.BeginForm("Index", "Employee", FormMethod.Post))
{
foreach (var department in Model.Departments)
{
#Html.RadioButtonFor(p => p.SelectedDepartments, department.DepartmentName)#department.DepartmentName
}
<br />
<input type="submit" value="Submit" />
}
This topic is widely descibed here: Correct way to bind an mvc3 radiobutton to a model.
But if you want quick solution, you should use this overload of RadioButtonFor which last parameter is "htmlAttributes". You can pass "checked" attribute there, so your code will look like this:
foreach (var department in Model.Departments)
{
var checked = departament.IsSelected ? new {checked="checked"} : new {};
#Html.RadioButtonFor(p => p.SelectedDepartments, department.DepartmentName,departament.DepartamentName,checked)#department.DepartmentName
}
i have a table called
tb_role
id role
1 admin
2 user
3 viewer
and for the View is like this :
<div style="width:50%; float:right;">
<legend>User Role</legend>
<table>
<tr>
<th>Role</th>
</tr>
<tr>
<td align="center"><input type="checkbox" id="CBRole"/></td>
</tr>
</table>
</div>
I want to ask, how to list my checkbox(CBRole) from my table? so my CBRole is listed from my table.
thanks a lot
EDIT
assumed that i have Roles table like this :
tb_role
RoleId Role_Name
1 SalesCreate
2 SalesEdit
3 AgentCreate
4 AgentEdit
i want to list role for Sales in checkbox (SalesCreate and SalesEdit, so its only have 2 checboxes), how to do that ?
thanks
From your controller you populate your view model with these properties:
Your RoleViewModel
public IList<int> RolesSelected { get; set; }
public MultiSelectList Roles { get; set; }
From controller that handles the Get call (/roles/edit/1 for example)
model.RolesSelected = new List<int>();
//here the code to populate the eventually already selected roles (update case)
model.Roles = new MultiSelectList(repository.GetRoles(), "Id", "Name", model.SettoriSelected);
then in your view (inside a form tag) you will do something like this
#foreach (var item in Model.Roles)
{
<div class="MyClass">
<label for="#item.Value" class="MyClassForCheck">
<input type="checkbox" id="#item.Value" name="RolesSelected" value="#item.Value" #(item.Selected ? "checked" : "") />#item.Text</label>
</div>
}
in the controller that answer the Post part you will access the RolesSelected with the IDs checked
In the example I have put a div, but yuo can change it to what you like obviously. Hope it helps
You probably want to have something that looks like the following post on StackOverflow, Enum to CheckBox
I have 4 parameters to filter (two dropdownlists and two datetime type fields) data from my database. I take data from 3 tables of this database. The view should be like the view on the screen. How can I do it? Simple gridview don't present data like i want.
How can I send 4 parameters to controller?
I am using Oracle Database
As an option you can make select with LINQ:
var list = (from t1 in DataContext.Tables1
//here add you joins
select new
{
Code = t1.Code,
CompanyName = t1.Company, //for example
//...
}).ToList();
GridView.DataSource = list;
GridView.DataBind();
But this will work only if you use "hybrid" application - both classic ASP and MVC approach. Otherwise you need pass list to your view.
About parameters... For example you have layout
<select name="filter1">
//...
<select>
<select name="filter2">
//...
<select>
<input type="hidden" name="dateStart" />
<input type="hidden" name="dateEnd" />
Then in you action you will have:
public ActionResult MyAction(int filter1, string filter2, DateTime? dateStart, DateTime? dateEnd)
{
//code here
return View();
}