Selectlist manipulation to merge 2 sets together in a single list - asp.net-mvc-4

ViewBag.WList = new SelectList(_bdb.BMW.OrderBy(o => o.NAME).AsEnumerable(), "ID", "NAME");
This is how I've setup my selectlist in the controller with below the view code.
#Html.DropDownList("WOptions", (SelectList)ViewBag.WList, "Select an option", new { style = "width:250px;" })
I have a table in my database which I can't add new records too and which I need to reference as my main option list, additionally I need to add 2 new records to reference exceptions.
Basically on my create form I have my option list and a type list since 1 of the options isn't suitable so 2 new choices have been added as a different dropdown in the form of the type list.
It's mostly not a problem to record this or display it but I have a filter on my kendo list based on the code above.
I'm wondering what the best method of adding in 2 options to the ward list would be if I can do it with my existing code or need to use a different method?
Additionally I've tried,
SelectList options = new SelectList(_bdb.BMW.OrderBy(o => o.NAME).AsEnumerable(), "ID", "NAME");
options.ToList().Insert(11, (new SelectListItem { Text = "Example1", Value = "11" }));
options.ToList().Insert(22, (new SelectListItem { Text = "Example2", Value = "22" }));
ViewBag.WList = options;
While this does still display the original selectlist it doesn't display the new items and this has so far been the only variant which didn't cause errors.
Any ideas or suggestions would be much appreciated.

The Linq Extension .ToList() returns a new instance of List. So options.ToList() does not reference to the internal list and that is why the later two items are not inserted to the internal list. To go around,
var bmws = _bdb.BMW.OrderBy(o => o.NAME).Select(x => new { ID = x.ID, NAME = x.NAME }).ToList();
bmws.Insert(11, (new { ID = "11", NAME = "Example1" }));
bmws.Insert(22, (new { ID = "22", NAME = "Example2" }));
var options = new SelectList(bmws.AsEnumerable(), "ID", "NAME");
I have not tested the code, but this should do the trick.
Hope this helps.

Related

How to get dropdown text value and text name from viewBag in MVC

Hello friends this really basic question in mvc, but iam lot of search i couldn't find it.
Control:
if (objResponse.ErrorNo == 0)
{
lstReason.Add(new SelectListItem { Value = "1", Text = "Select" });
foreach (XElement xEle in resXml.Descendants("R"))
{
lstReason.Add(new SelectListItem { Value = xEle.Element("C1").Value, Text = xEle.Element("C2").Value });
}
ViewBag.ReasonCode = lstReason;
}
}
View:
#Html.DropDownListFor(m => m.ReasonCode, new SelectList(ViewBag.ReasonCode, "Value", "Text"), new { #class = "form-control autofocus", #id = "ddlReason" })
I got it only m => m.ReasonCode but how can get m.reason from ViewBag.ReasonCode text value.
Any help would be really appreciated.
Your ViewBag member cannot be named the same as your model property. The "selected" value is determined from ModelState, which is composed of values from Request, ViewData/ViewBag and finally Model. As a result, you're essentially saying the that "selected" value should be the entire list of SelectListItems, which obviously does not match an item in that list. Change your ViewBag member to something like ViewBag.ReasonCodeOptions, and you'll be good.

Dojo gridx: using "onAfterRow" disables "onCellWidgetCreated"?

When creating a gridx, I use the following column definition to insert an Edit button in to the last cell of each row:
var editColumn = { field : 'Edit', name : '', widgetsInCell: true,
onCellWidgetCreated: function(cellWidget, column){
var btn = new Button({
label : "Edit",
onClick : function() {
console.log('Do stuff here');
});
btn.placeAt(cellWidget.domNode);
}
};
columns.push(editColumn);
var grid = new Grid({
cacheClass : Cache,
store : store,
structure : columns,
modules: ["gridx/modules/CellWidget"]
}, 'gridNode');
grid.body.onAfterRow = function(row){
...do stuff on the row here
};
Whne I include the onAfterRow function the row processing happens but the OnCellWidgetCreated does not. Each function seems wo work in absence of the other. Any suggestions on how I can: (1) format the rows according to row data AND (2) insert the button widgets in the last cell of each row?
Ok, solved it. Rather than assign the grid.body.onAfterRow, the way that worked for me was:
aspect.after(grid.body,'onAfterRow',function(row){
key = row.id;
if ('anulada' in row.grid.store.get(key)){
if(row.grid.store.get(key).anulada == true){
row.node().style.color = 'gray';
}
}
},true);
You need to require "dojo/aspect".

facets with ravendb

i am trying to work with the facet ability in ravendb but getting strange results.
i have a documents like :
{
"SearchableModel": "42LC2RR ",
"ModelName": "42LC2RR",
"ModelID": 490578,
"Name": "LG 42 Television 42LC2RR",
"Desctription": "fffff",
"Image": "1/4/9/8/18278941c",
"MinPrice": 9400.0,
"MaxPrice": 9400.0,
"StoreAmounts": 1,
"AuctionAmounts": 0,
"Popolarity": 3,
"ViewScore": 0.0,
"ReviewAmount": 2,
"ReviewScore": 45,
"Sog": "E-TV",
"SogID": 1,
"IsModel": true,
"Manufacrurer": "LG",
"ParamsList": [
"1994267",
"46570",
"4134",
"4132",
"4118",
"46566",
"4110",
"180676",
"239517",
"750771",
"2658507",
"2658498",
"46627",
"4136",
"169941",
"169846",
"145620",
"169940",
"141416",
"3190767",
"3190768",
"144720",
"2300706",
"4093",
"4009",
"1418470",
"179766",
"190025",
"170557",
"170189",
"43768",
"4138",
"67976",
"239516",
"3190771",
"141195"
],
}
where the ParamList each represents a property of the product and in our application we have in cache what each param represents.
when searching for a specific product i would like to count all the returning attributes to be able to add the amount of each item after the search.
After searching lg in televisions category i want to get :
Param:4134 witch is a representative of LCD and the amount :65.
but unfortunately i am getting strange results. only some params are counted and some not.
on some searchers where i am getting results back i dont get any amounts back.
i am using the latest stable version of RavenDB.
index :
from doc in docs
from param in doc.ParamsList
select new {Name=doc.Name,Description=doc.Description,SearchNotVisible = doc.SearchNotVisible,SogID=doc.SogID,Param =param}
facet :
DocumentStore documentStore = new DocumentStore { ConnectionStringName = "Server" };
documentStore.Initialize();
using (IDocumentSession session = documentStore.OpenSession())
{
List<Facet> _facets = new List<Facet>
{
new Facet {Name = "Param"}
};
session.Store(new FacetSetup { Id = "facets/Params", Facets = _facets });
session.SaveChanges();
}
usage example :
IDictionary<string, IEnumerable<FacetValue>> facets = session.Advanced.DatabaseCommands.GetFacets("FullIndexParams", new IndexQuery { Query = "Name:lg" }, "facets/Params");
i tried many variations without success.
does anyone have ideas what am i doing wrong ?
Thanks
Use this index, it should resolve your problem:
from doc in docs
select new {Name=doc.Name,Description=doc.Description,SearchNotVisible = doc.SearchNotVisible,SogID=doc.SogID,Param = doc.ParamsList}
What analyzer you set for "Name" field. I see you search by Name "lg". By default, Ravendb use KeywordAnalyzer, means you must search by exact name. You should set another analyzer for Name or Description field (StandardAnalyzer for example).

Conditional patch requests in RavenDB

How do I patch a document in RavenDB conditionally. The below code just patches all documents of type patron to Middle Initial = JJJ. I also would like to do this per condition.. for example .. do the same patch for the same Patrons documents types.. but for only those that have City ="New York"
store.DatabaseCommands.UpdateByIndex("Raven/DocumentsByEntityName",
new IndexQuery { Query = "Tag:Patrons" },
new[]
{
new PatchRequest
{
Type = PatchCommandType.Set,
Name = "MiddleInitial",
Value = "JJJ"
}
}, allowStale: false);
ZVenue,
You do it using:
store.DatabaseCommands.UpdateByIndex("Patrons/ByCity",
new IndexQuery { Query = "City:\"New York\"" },
new[]
{
new PatchRequest
{
Type = PatchCommandType.Set,
Name = "MiddleInitial",
Value = "JJJ"
}
}, allowStale: false);
Where the Patrons/ByCity index is defined as:
from p in docs.Patrons select new { p.City }
EDIT: It seems that I've been wrong with this answer, because Ayende explains a way how to do it in his answer.
This is something that cannot be done at the moment. However, Matt Warren has implemented something based on IronJS to do this stuff. I don't know when and if it will become part of the main product, but you can certainly use his Github repo if you really need it.
Instead, I suggest you either patch the documents on your own or don't denormalize the data and use .Include() instead if that's applicable in your case.

Organising a Lunene Directory

I have a set of records. Each record can have multiple skills and a single state.
So you might have a record of skills a, b and c and a state of Victoria.
I need to be able to search the directory for any records that have say skill a in Victoria or skill a and c in Victoria.
I'm having trouble creating an effective directory that will allow me to search in the manner I want.
At first i created a directory with skills: a b c state:vic
then i tried skills: a,b,c state:vic
But searching these is not giving me the correct results. In fact when i have a query;
skills:a,b AND state:vic,
skills: a OR b AND state:vic,
skills: a OR skills:b AND state:vic
the above all return a set of records for skills: a AND state:vic.
Any thought?
EDIT
Since posting this I have gotten it to work but am unsure if this is the right approach.
I have combined all the skills into a single field and space seperated them. The skills are in GUID format.
Then in my search method I do this;
queryString = "skills:(skill1 OR skill2 OR skill3) AND state:Vic";
Query query = parser.Parse(queryString);
This works fine and it's very quick but is creating a queryString really the way to go with this or is there a better way?
You can use a BooleanQuery : http://geekswithblogs.net/rgupta/archive/2009/01/07/lucene-multifield-searches.aspx
You could build your BooleanQuery directly, instead of going via a QueryParser. This requires that you differentate skills- and state input fields when presenting your search interface to your users.
var skillQuery = new BooleanQuery {
{ new TermQuery(new Term("skills", "skill1")), Occur.SHOULD },
{ new TermQuery(new Term("skills", "skill2")), Occur.SHOULD },
{ new TermQuery(new Term("skills", "skill3")), Occur.SHOULD }
};
var query = new BooleanQuery() {
{ skillQuery, Occur.MUST },
{ new TermQuery(new Term("state", "Vic")), Occur.MUST }
};
// query = "+(skills:skill1 skills:skill2 skills:skill3) +state:Vic"
Allowing freetext entries when searching for skills still require a call to QueryParser.Parse.
var analyzer = new StandardAnalyzer(Version.LUCENE_30);
var skillParser = new QueryParser(Version.LUCENE_30, "skills", analyzer);
var skillQuery = skillParser.Parse("skill1, skill2, skill3");
var query = new BooleanQuery() {
{ skillQuery, Occur.MUST },
{ new TermQuery(new Term("state", "Vic")), Occur.MUST }
};
// query = "+(skills:skill1 skills:skill2 skills:skill3) +state:Vic"