I am very new to MVC in general, so this might sound very silly. However, I don't quite get how to connect my data in the following scenario:
I am creating a project where I have basically two tables
Table 1
pid | pname | etc
Table 2
id | pid | etc
Now, I have successfully implemented a controller where I populate a list with the data in table 1 using "date range" as a parameter.
URL 1
/table1object?search=Search&endDate=2015-02-20&fromDate=2015-02-20
As the tables shown, each row in this view is connected to a certain row in table 2. So, for this I am generating a link which basically sends me to the details of the second controller by looping through the current model.
#Html.ActionLink("Details", "Details/" + #result.pId, "table2Object", null)
Now, my controller number 2 (Details) is only accepting an id to display that specific information of the Table 2.
URL 2
/table2object/Details/96
The Question
Since I am only passing this id to access the second controller, I am going to be able to see only one item at a time. What if I want to add a previous and next buttons to navigate through items in the second controller, but in the range established in the first controller? How can I do this?
Thank you
A first way to do it without respecting any best practices is to save your search result (Controller1) inside a Session variable.
Session["CurrentResult"] = YourViewModelInController1 ;
and then in Controller 2
ActionResult Details(int id)
{
var prevViewModel = Session["CurrentResult"] ;
// Do what you want to find the prev and the next from prevViewModel
}
Of course you can't get the next and the prev if you access your DetailController without displaying table1.
And also, there will be many side effects. e.g : when user make 2 search at once, ..
The second good and correct way to do it, is to re-execute a search to find the previous and the next on Controller 2 Details. Independently of the previous
ActionResult Details(int id)
{
// Do a real search to find the next and the previous
}
Considering you have the idea how to pass values and work in a controller, i can give you a suggestion as below:
You can load your items in a list within a given date range first. Pass that list to 2nd controller And then traverse that list by clicking prev and next using a counter.
Hope this helps,
Thanks
Related
my following problem is, that I have a List of Items and want to index those with elasticsearch. I have a running elasticsearch instance, and this instance has an index called "default".
So I'm running following code:
var items = GetAListOfItem();
var response = Client.IndexMany(items);
I also tried it with Client.IndexManyAsync(items). But that didn't do anything.
Only 1 Item of this List gets indexed. Nothing more. I think its the last item, which got indexed.
I thought it could be a thing with IEnumerable and multiple enumerations, but i parsed it as a List<Item>.
Another Question would be about the best practice with Elasticsearch. Is it common to use a Index per Model. So if I'm gathering data from for example Exchange and another system, I would do 2 indeces?
ExchangeIndex
OtherSystemIndex
Thank you for your help.
Update: I saw that my Client.Index does all those calls succesful, but all those objects got the same ID from NEST. Normally she had to increment by herself, isnt it?
Update 2: I fixed the Indexing Problem. I had setup an empty ID-Field.
But still have the question mit best practive about Elasticsearch.
If you are uploading all the data with the same id, it will not increment the id, that will update the record with that id and you will have only one record, so you can upload the data without an id or give wherever unique id to identified the records.
The other common problem is that your records have not the same mapping that you give for the index.
About the other question, in the indexes, you store the information that is relevant for you, even if that have content from many models, the only thing that you have to avoid is mix information, if you have an index about server logs dont mix it with user activities for example.
I have a session in my Login Controller and there i send List of details via Session to the another page.So i need to take one item of the list to show in mvc view(Actially i need to show UserName in my mvc view).
My Controller
public ActionResult Login(User usrdtl, string returnUrl)
{
//some code here
List<UserDtl> usrList = newsManager.GetUserdetailsByuId(usrdtl.uId);
System.Web.HttpContext.Current.Session["UsrSession"] = usrList ;
}
This usrList consist of lots of user details,i need to show UserName in my view.
In my view i tries to take it as,
<span>#Session["UsrSession"] </span>
But it shows me error
System.Collections.Generic.List`1[NTL.Sys.Entities.UserDtl]
How can i get this ?
It actually does not show you error. It shows the object representation by calling .ToString() on the object.
In your case, Session["UsrSession"].ToString() returns System.Collections.Generic.List1[NTL.Sys.Entities.UserDtl].
Your code actually has no problem at all, modify it to show exactly what you want to show.
I assume that the list contains exactly 1 item. To show the UserName, try:
<span>#((Session["UsrSession"] as List<UserDtl>).First().UserName) </span>
How to show the list is up to you, if you want to display a list of Users, just loop through it. The above code is just an example.
I am working on Visual C# MVC project. I am using databse first approach in my model.My project is about developing an appmarket where users can buy apps. I used partial view in home page which will conatin details of different apps in different partial views.Like App1 in 1st partial view, App2 in 2nd partial view, so on. For this I used foreach loop.
In each partial view there is link called MoreInfo, so when user clicks on that they will go inside MoreInfo page. In database I have fields such as app info, app cost, description etc which will be displayed inside MoreInfo page. All these fields are in one table called Apps table.
My problem here is if i use forach loop inside MoreInfo page, then I am getting information of each record from database. To be more clear, in Database I have two records for Description field called "Description 1" and "Description 2". If i use foreach loop, then it is displaying two views, one for Description 1 and another for description 2 in same page.
What I want is Desription of 1st app in first MoreInfo page and second description in 2nd MoreInfo view and so on.
can someone help me in this?
Thanks..
Assuming you are using Entity Framework:
Enumerable.FirstOrDefault will do this for you. You can load it into the TSource type.
Here is a link: http://msdn.microsoft.com/en-us/library/bb340482.aspx
var someobj = db.someEnumerable.FirstOrDefault(x => // Condition);
If you're using LINQ to SQL or EF you can tack FirstOrDefault() onto your call which will return the first value or in most cases null (default value for reference types). If you're using something like SQLConnection or invoking a sproc just change your SELECT ... FROM to SELECT TOP 1 .... FROM
You should be using Linq to get one record as per your condition. try something like this (this is only code to show you the logic as we are not sure what variables you have used).
var result=(from x in dbContext.TableName where column==variable select x).FirstOrDefault()
I hope it will help.
I am using a Wizard control to capture users vehicles they own.
Stage 1 - Captures users name, address etc
Stage 2 - Captures only one vehicle (one vehicle consists of reg, model, mileage and manufacturer)
Stage 3 - Gives a summary of the details entered and saves the record when they click finish.
I would now like to capture multiple vehicles at stage 2.
Since the record is saved at stage 3 my guess is i need someway of storing each vehicle at stage 2 (which the user can view and edit if required before saving the record) and then passing all temporarily saved vehicles to my method at stage 3 to save it against the user.
How could i approach this? My concern with my research is that most of the methods available dont show how to store each vehicle temporarily?
You can use the Session variable to store the value if you are using the web application otherwise cache is also useful container. If your wizard is single page with multiple form you can also use the hidden field or viewState. Have a class with all the properties you want to store and use the class to put in the Session
public class MyData
{
public string Name;
public string Address;
}
In the first part of wizard do like this then once value is populated store in the Session before moving to next page. In next page so like this
MyData myData = (MyData]Session["myData"];
//again update rest of the data from current page and
Session["myData"] = myData;
//Go to next page
In my MVC 3 project (for a company that give lessons), I have about 100 clients in a database and the amount they pay per month is constant - it's either R640 or R460, depending on whether they're enrolled for a group or a private lesson. What I want to do is, have one button that when clicked, will generate database entries for invoices for each client for the current month - so will generate 100 invoices. Would this be difficult to do?
Not really.
you would have your button link to a controller action maybe called GenerateInvoices?
Then loop through each client and do the checks and the database writings you need done.
Just write the code and have the buttons action be the controller action.