I am having an issue with search/filter.
I have a list (user details - pic,name) and I use html helper editorformodel to display the list, I would like to incorporate search for the list, as I type in each letter I would like the list to display the matching items from the list without going back to controller. Is this possible?
I have seen some posts like Asp.Net MVC3 adding search functionality but they go back to controller
I am new to MVC, Please help.
Indira
Edit(additional info):
Here's what I was doing, I am passing a model(there are two lists and two strings) from controller to view, using Editorformodel() in view and creating partialview for model. In the partialview I display two lists( which in turn are models containing name,picture url ,bool value). I need to search through this list for matching string from the username's as we type in letters, and select those elements to pass to the model.
Example:
public class myuser
{string name;string picture_url; bool selected;}
public class mylibrary
{string name; IEnumerable<myuser> userlist; IEnumerable<myuser> adminuser; string deadline;}
controller{...... return view(mylibrary);}
In the model
#model ....models.mylibrary
.
.
.
#using(Html.BeginForm(....)
{
#Html.EditorForModel()
}
Partial view for mylibrary
#model ....models.mylibrary
#Html.EditorFor(x=>x.userlist) ---this is the list I want to search through
#Html.EditorFor(x=>x.adminuser)
#Html.TextBoxFor(x=>x.deadline)
partial view for myuser
#model .....models.myuser
#html.Checkboxfor(x=>x.selected)
#html.LabelFor(x=>x.Name)
I am not using any table to display, it's all html. I'd like to modify the list on every keystroke and return the updated list and that's were I'm struggling. Please let me know if you need more detail, and thanks again for the help.
Select To Autocomplete is a good jQuery plugin for achieving this functionality. It requires only a single line to turn a select list into an autocompletable input
$('select').selectToAutocomplete();
Related
I have a razor view where the model is an IEnumerable of Product so I am simply listing all of the products in the view using a foreach loop on the Model. Along with each product you can see below that I am showing the count of how many ThumbsUps it has. The final thing I am trying to accomplish is when they click on the link 'Give Thumbs Up' I would like to add a ProductThumbsUp record to the database for the current logged in user. If the user already has given a thumbs up and clicks it again I would like to remove their ProductThumbsUp from the product.
I am using EntityFramework 7 and MVC6. I have two tables: Product and ProductThumbsUp.
I would like to know how I should go about adding a ProductThumbsUp record for the current logged in user when they click the link using MVC. I am assuming something like this should definitely be an AJAX post. But would I simply just want to send the ProductId to the AJAX post? Then in my controller create a new ProductThumbsUp and set the ProductId of it. Then send both the newly created ProductThumbsUp object and the current logged in user down to my repository layer for saving.
I am wondering if there are better ways to do this since each post in my model already has a list of ThumbsUps attached to it. Is there a better way I can be doing this?
Here is how my razor view is setup:
#model IEnumerable<Product>
#foreach (var product in Model)
{
<h2>#product.Name</h2>
<div>#product.Description</div>
# of Thumbs Ups Given:<span class="badge">#product.ThumbsUps.Count()</span>
Give Thumbs Up
}
Here is what my Product model looks like:
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<ProductThumbsUp> ThumbsUps { get; set; }
To make it efficient,
I would recommend getting all the products and instead of getting all the rows of thumbsUps for each product from DB, just get the like count for each product.
As you dont need each row of thumbsUp but just the count to display. Imagine the scenario for scale if you have a million likes on a product, suddenly you are pulling a million rows from the DB to your server - just to show a count.
Also i would make a second call to the database to get all the thumbsUps the current user has given (a list of product Ids he has liked). That will allow you to display to the user if they have liked it or now.
To add the acutal thumbs up to the product - you can send an ajax call which tells which productId he liked and mvc action can insert a thumbsUp row to the database.
Easiest way to do it is Create Two anchor tags
Give Thumbs Up
Remove Thumbs Up
And toggle it using javascript or jquery.
Similarly call method using jquery ajax to add and remove the count(can use the same method using a flag)
Using the same method you can return the new Count and bind it
I have around 20 cities in my database. I need to develop a page where I can allocation some value to each city. I can update the allocation in the page.
My Data base looks like below
City table - > City(CityId, Name)
I have a table to store the allocation for cities
Table - > CityAllocation(CityId, Allocation)
I have to display a text box near all the city names.
CityAllocationViewModel.cs
public class CityAllocationViewModel
{
public List<City> Cities{get;set;}
public List<CityAllocation> CityAllocations{get;set;}
}
CityAllocation.cshtml
Step 1: To display all the cities I will loop through Cities list and display it.
Step 2: Need to display text boxes near each of the city -> which will load existing values from CityAllocations if anything is there. Otherwise just need to display empty
But for the first time CityAllocations list will be null. Can someone explain me how to construct text box with proper binding name so that when I save it, the values gets properly binded to action method parameter. I am using MVC 4.
My Action method looks like below
CityController.cs
public ActionResult SaveCityAllocation(CityAllocationViewModel cityAllocationViewModel)
{
}
I will suggest use EditorTemplateFor . Create a partial View for CityAllocation and call in main view CityAllocationViewModel with as #html.EditorFor(m=>m.CityAllocations)
Hello All: I've been struggling with parts of MVC, especially using data from the controller in my View when my view has multiple side by side divs where I need to display model data/entities.
Left Div (Client One) Right Div (Client Two)
Left Div (Client Three) Right Div (Client Four)
I am passing a simple model to my view from the controller. No matter how I try, it seems that the only way to access the "entities"/rows in the model is via a foreach and iterating over them sequentially. I really need to get "entities" by their index value, by turning the model data from an IEnumerable into an array and getting entity[ n ].
Is this possible or do I not understand the relationship between the View and the model data.
Here's my controller:
public ActionResult ClientView()
{
return View("ClientView", db.Clients.ToList());
My model is a simple list of typical client properties with no navigation: FirstName, LastName, Address, etc. In the view, I'm trying to get client 1 in a left side div, and client 2 in a right side div, then a new row and start over.
Thanks much in advance
Allen
Suppose your view is strongly typed to List:
#Model List<MySoftware.Models.Client>
Then you can access the clients by index:
Model[0]
Model[1]
Model[2]
This is possible because the IList interface allows you to retrieve items by index, just like an array. A List is generally the preferred method as arrays are... well... a bit old fashioned.
However, a better design might be to simply work this into your UI code instead, since this is really a UI display issue. Something like:
#Model IEnumerable<MySoftware.Models.Client>
var i = 0;
foreach (var client in Model)
{
<div> [client details] </div>
i++;
if (i % 2 == 1)
<br/>
}
I'm building a page in which the user can query a set of data by building up a set of search criteria, something like the way the Visual Studio TFS plugin lets you search work items: a table of conditions, where you can keep adding rows. You select "and" or "or" for the join condition, then select a field, enter a value, and select whether you want things that do or do not match it:
1. show items where [Field] [is|is not] [value]
2. [and|or] [Field] [is|is not] [value]
3. [and|or] [Field] [is|is not] [value]
etc...
Now, I'm looking at ways to build this, and I had a thought. In the past, I've used Knockout, but this requires me to have models in Javascript to map the data to, which seems redundant when I already have those models in C# in the server-side code. Of course, I can use Razor code to foreach through a list of criteria that's part of the model in a strongly-typed view, but I can't find a tidy way to add to this list.
The model structure in C# is (roughly) like this:
Field:
field name
list of options for the value
boolean value for the is/is not option.
Criterion:
Field
selected value
combination type (and/or)
Query:
list of Criterions (that looks weird not saying Criteria)
start and end date
the user's access level
view field and sorting options
QueryViewModel:
Query
assorted lists to populate the view options selection area
a little metadata for other (unrelated) displays on the page
In Knockout, I'd add an on-click method to the "add search criteria" button to add new entries to the list of criteria. Can I use a Razor functions block (#functions { ... }) to achieve a similar result? I've tried a few things, but I either find that the viewmodel doesn't seem to be in scope, or that there's no way to update the page to show the new contents of the viewmodel (although I'm experimenting with something that involves passing the newly-updated viewmodel to a partial view, which might work somehow). Can this be done, or do I need to take a deep breath and go back to the Javascript?
You can serialize/deserialize your C# objects as JSON, either with standard MVC controllers and JSONResult or with Web API in MVC 4. This means you dont have to explicitly redefine your c# objects in javascript.
In the browser you can use the knockout mapping plugin to make your json properties into knockout observables if required.
This is generally a cleaner and more robust approach than dynamically loading and rendering html from the server (if thats what you were suggestiong - wasnt 100% clear from your post).
I am building an MVC app that will display a bit of data at screen. I might end up having hundreds or maybe even thousands of data.
So I'm using a PagedList format following this tutorial:
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
Which helped me. The Paged List works. But I have one problem that I can't seem to figure out how to solve. In the tutorial, there's only 1 parameter, a textbox, and the tutorial keeps the value "in memory" by keeping and attributing the ViewBag parameter so that changing page does not "flush" the text in the textbox.
Right now I'm building something like a search engine with, right now, 23 parameters and counting. Amongst these parameters are checkboxes, textboxes, and dropdownlists.
My question: is there any way to keep these in memory? How may I proceed? Must I keep them all in memory just like the tutorial does?
Thank you very much!
Well, in the tutorial, they are not really "kept in memory" between requests. In each request, the value is passed into the action method, which then passes it to the view where it is used in creating the paging and sorting links.
Previously, when paging, instead of adding each parameter individually, I simply have a method that reads the querystring and builds the link using each provided parameter:
NameValueCollection queryString = helper.ViewContext.HttpContext.Request.QueryString;
foreach (string key in queryString)
{
if (key != null)
{
if (!newValues.ContainsKey(key))
{
if (!string.IsNullOrEmpty(queryString[key]))
{
newValues[key] = queryString[key];
}
}
}
}
Then, to create the link, I use:
string link;
if (!string.IsNullOrEmpty(routeName))
{
link = helper.RouteLink(text, routeName, newValues).ToString();
}
else
{
actionName = actionName ?? values["action"].ToString();
controllerName = controllerName ?? values["controller"].ToString();
link = helper.ActionLink(text, actionName, controllerName, newValues, null).ToString();
}
return string.Concat(" ", link);
If you are using AJAX techniques, then you can create 2 action methods in your controller.
The first should return the whole view and the second should return a partial view.
The first will contain your search controls (all 23 of them). These will be rendered only the first time.
The second one will return the partial view and will be parameterized by the 23 odd parameters you have to your search query. It might be an idea to stuff them all in a ViewModel class and let the default model binder bind it at run time.
Finally, you can then load the results part of the search results page by an AJAX call to the 2nd method using JavaScript/jQuery.